ajax.js

var state= new Array(); state[0]="Request not initialized"; state[1]="Server connection established"; state[2]="Request received"; state[3]="Processing request"; state[4]="Request finished and response is ready"; var status= new Array(); status[200]="OK"; status[404]="Page not found"; function ajax(adres, id){ var number = Math.round(Math.random()*10000); if(adres.indexOf("?")==-1){ adres=adres + '?num=' + number ; } else{ adres=adres + '&num=' + number ; } addAjaxHtmlTop("<p>Adres:<span class='adres'> " + adres + "</span> (" + id + ")</p>"); var req = new XMLHttpRequest(); req.open("GET", adres, true); req.send(); req.onreadystatechange = function(){ verwerken(req, id) }; } function verwerken(req,id) { try{ addAjaxHtmlTop("<div>Readystate: " + req.readyState + " | " + state[req.readyState] + "</div>"); if(req.status != 200){ addAjaxHtmlTop("<div> Status: " + req.status + " | " + status[req.status] + "</div>"); } if (req.readyState==4 && req.status==200) { //alert(req.responseText); if(document.getElementById('alertRespons') && document.getElementById('alertRespons').checked){ alert(req.responseText); } if(document.getElementById(id)){ document.getElementById(id).innerHTML = req.responseText; } else{ alert('id '+ id + ' niet gevonden') } addAjaxHtmlTop("<div align='center'>--------------------------------------------------</div>"); } else{ } } catch(err){ alert("Javascriptfout: " + err) } } function addAjaxHtmlTop(html){ if(document.getElementById('ajaxtext')){ document.getElementById('ajaxtext').innerHTML = html + document.getElementById('ajaxtext').innerHTML } }

Resultaat

Made by Thijs Aarnoudse