1 | function 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 | |
---|
8 | function 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 = handleResponse(xmlhttp, url, target, callback); |
---|
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? |
---|
29 | function handleResponse(xmlhttp, url, target, callback) { |
---|
30 | if (xmlhttp.readyState == 4) { |
---|
31 | if (xmlhttp.Status == 200) { |
---|
32 | callback(xmlhttp, target); |
---|
33 | //document.getElementById(target).innerHTML = xmlhttp.responseText; |
---|
34 | } |
---|
35 | else { |
---|
36 | //document.getElementById(target).innerHTML = "loadExternalContent error: \n" + xmlhttp.status + "\n" + xmlhttp.statusText; |
---|
37 | } |
---|
38 | } |
---|
39 | } |
---|