What is AJAX?
webtechnology, ajax, Asynchronous JavaScript and XML

Using technologies like AJAX (Javascript + Javascript)

Ajax is a marketing gag because in essence it is Javascript neatly wrapped up as "Asynchronous JavaScript and XML" and it doesn't pertain to the Trojan hero Ajax. At least that's what I thought when I first heard that name.

First we need a JavaScript context (that is, we create an object, required to get data).


<script>
var context=null;
context = new XMLHttpRequest();
</script>

Then we shall open the file from the server asynchronously. This means that the file is loaded in the background and the user won't have to be kept waiting if it is a very huge file. The important issue is that you don't have to request the whole site again. You are requesting just some content which we insert dynamically into an empty tag with the id "display". You as a user have already loaded this page into your browser, and then there goes Ajax loading another tidbit via javascript. The text "Everything can be made easy." is loaded from a text file into the empty div:

<div id="display"></div><!--This div is still empty. After the script has been executed the "innerHTML" is filled by Ajax-->

<script>
if (context !=0)
   {
   context.open("GET", "../examples/easy.txt", true);//GET not POST,name of the file, true for asynchronous loading
   context.onreadystatechange = show_me;//if something has changed call "show_me" checking if state "4" has been reached (meaning success)
   context.send(null);//no parameters handed over to "examples/easy.txt"
   } 
  
function show_me() {
if(context.readyState==4) //"4" means that the request has been completed
      {
      document.getElementById("display").innerHTML = context.responseText;//put something into the div with the id "display"                    
      }
}     
</script>

You can call the file "examples/easy.txt" to check.


Primitivecode ist Grammatik pur. Aber Programmieren kommt auch nicht zu kurz, denn die Seite mache ich allein. Informationen zum Datenschutz findest du hier.