source: Dev/trunk/js/ajax.js @ 156

Last change on this file since 156 was 156, checked in by fpvanagthoven, 13 years ago
File size: 1.3 KB
Line 
1function loadContent (query, url, target, callback) {
2    newAjaxRequest(query, url, target, handleResponse());
3    return false;
4}
5
6// Waar is bovenstaande tussenstap voor nodig..? Hij doet niets anders dan redirecten naar loadExternalContent en false returnen.
7
8function newAjaxRequest(query, url, target, callback) {
9    var xmlhttp;
10    document.getElementById(target).innerHTML = 'Fetching data...';
11    if (window.XMLHttpRequest) {
12        xmlhttp = new XMLHttpRequest();
13    }
14    else if (window.ActiveXObject) {
15        xmlttp = new ActiveXObject("Microsoft.XMLHTTP");
16    }
17    else {
18        document.getElementById(target).innerHTML = "AJAX not supported!";
19    }
20   
21    if (xmlhttp !== undefined) {
22        xmlhttp.onreadystatechange = callback(xmlhttp, url, target);
23        xmlhttp.open("POST", url, true);
24        xmlhttp.send(query);
25    }
26}
27
28//Handle response is een soort standaard response functie, is het nodig om deze te kunnen overriden met een custom callback functie?
29function handleResponse(xmlhttp, url, target) {
30    if (xmlhttp.readyState == 4) {
31        if (xmlhttp.Status == 200) {
32            document.getElementById(target).innerHTML = xmlhttp.responseText;
33        }
34        else {
35            document.getElementById(target).innerHTML = "loadExternalContent error: \n" + xmlhttp.status + "\n" + xmlhttp.statusText;
36        }
37    }
38}
Note: See TracBrowser for help on using the repository browser.