1 /* 2 3 Ajax Gold JavaScript Library 4 ** No warranty is expressed or implied. ** 5 6 The Ajax Gold JavaScript Library executes in a web browser and allows 7 you to fetch data from the server behind the scenes, using JavaScript 8 without having the browser reload the current page (which would cause 9 the screen to flicker and reset while waiting for the page to be 10 loaded from the server), which is what Ajax is all about. This 11 library has been designed to be thread-safe. 12 13 To use this JavaScript library in your own web pages, place 14 ajaxgold.js in the same directory as your web pages and use this 15 line in the <head> section of your pages: 16 17 <script type = "text/javascript" src = "ajaxgold.js"></script> 18 19 This library supports these functions for using Ajax (most commonly 20 used: getDataReturnText and getDataReturnXml): 21 22 getDataReturnText(url, callback) 23 ** Uses the GET method to get text from the server. ** 24 Gets text from url, calls function named callback with that text. 25 Use when you just want to get data from an URL, or can easily 26 encode the data you want to pass to the server in an URL, such as 27 "http://localhost/script.php?a=1&b=2&c=hello+there". 28 Example: getDataReturnText("http://localhost/data.txt", doWork); 29 Here, the URL is a string, and doWork is a function in your own 30 script. 31 32 getDataReturnXml(url, callback) 33 ** Uses the GET method to get XML from the server. ** 34 Gets XML from url, calls function named callback with that XML. 35 Use when you just want to get data from an URL, or can easily 36 encode the data you want to pass to the server in an URL, such as 37 "http://localhost/script.php?a=1&b=2&c=hello+there". 38 Example: getDataReturnXml("http://localhost/data.txt", doWork); 39 Here, the URL is a string, and doWork is a function in your 40 own script. You can recover XML elements from the XML object 41 passed to your callback function using JavaScript methods like 42 getElementsByTagName. 43 44 postDataReturnText(url, data, callback) 45 ** Uses the POST method to send data to server, gets text back. ** 46 Posts data to url, calls function callback with the returned text. 47 Uses the POST method, use this when you have more text data to send 48 to the server than can be easily encoded into an URL. 49 Example: postDataReturnText("http://localhost/data.php", 50 "parameter=5", doWork); 51 Here, the URL is a string, the data sent to the server 52 ("parameter=5") is a string, and doWork is a function in 53 your own script. 54 55 postDataReturnXml(url, data, callback) 56 ** Uses the POST method to send data to server, gets XML back. ** 57 Posts data to url, calls function callback with the returned XML. 58 Uses the POST method, use this when you have more text data to send 59 to the server than can be easily encoded into an URL. 60 Example: postDataReturnXml("http://localhost/data.php", 61 "parameter=5", doWork); 62 Here, the URL is a string, the data sent to the server 63 ("parameter=5") is a string, and doWork is a function in 64 your own script. You can recover XML elements from the XML object 65 passed to your callback function using JavaScript methods like 66 getElementsByTagName. 67 68 Bear in mind that the URL you want to fetch data from has to be in 69 the same domain as your web page that uses Ajax Gold methods or, as 70 with any Ajax application, you'll get a security warning. If you want 71 to fetch data from another domain, have your server-side program do 72 the fetching and send the fetched data back to your Ajax application. 73 74 */ 75 76 function getDataReturnText(url, callback) 77 { 78 var XMLHttpRequestObject = false; 79 80 if (window.XMLHttpRequest) { 81 XMLHttpRequestObject = new XMLHttpRequest(); 82 } else if (window.ActiveXObject) { 83 XMLHttpRequestObject = new 84 ActiveXObject("Microsoft.XMLHTTP"); 85 } 86 87 if(XMLHttpRequestObject) { 88 XMLHttpRequestObject.open("GET", url); 89 90 XMLHttpRequestObject.onreadystatechange = function() 91 { 92 if (XMLHttpRequestObject.readyState == 4 && 93 XMLHttpRequestObject.status == 200) { 94 callback(XMLHttpRequestObject.responseText); 95 delete XMLHttpRequestObject; 96 XMLHttpRequestObject = null; 97 } 98 } 99 100 XMLHttpRequestObject.send(null); 101 } 102 } 103 104 function getDataReturnXml(url, callback) 105 { 106 var XMLHttpRequestObject = false; 107 108 if (window.XMLHttpRequest) { 109 XMLHttpRequestObject = new XMLHttpRequest(); 110 } else if (window.ActiveXObject) { 111 XMLHttpRequestObject = new 112 ActiveXObject("Microsoft.XMLHTTP"); 113 } 114 115 if(XMLHttpRequestObject) { 116 XMLHttpRequestObject.open("GET", url); 117 118 XMLHttpRequestObject.onreadystatechange = function() 119 { 120 if (XMLHttpRequestObject.readyState == 4 && 121 XMLHttpRequestObject.status == 200) { 122 callback(XMLHttpRequestObject.responseXML); 123 delete XMLHttpRequestObject; 124 XMLHttpRequestObject = null; 125 } 126 } 127 128 XMLHttpRequestObject.send(null); 129 } 130 } 131 132 function postDataReturnText(url, data, callback) 133 { 134 var XMLHttpRequestObject = false; 135 136 if (window.XMLHttpRequest) { 137 XMLHttpRequestObject = new XMLHttpRequest(); 138 } else if (window.ActiveXObject) { 139 XMLHttpRequestObject = new 140 ActiveXObject("Microsoft.XMLHTTP"); 141 } 142 143 if(XMLHttpRequestObject) { 144 XMLHttpRequestObject.open("POST", url); 145 XMLHttpRequestObject.setRequestHeader('Content-Type', 146 'application/x-www-form-urlencoded'); 147 148 XMLHttpRequestObject.onreadystatechange = function() 149 { 150 if (XMLHttpRequestObject.readyState == 4 && 151 XMLHttpRequestObject.status == 200) { 152 callback(XMLHttpRequestObject.responseText); 153 delete XMLHttpRequestObject; 154 XMLHttpRequestObject = null; 155 } 156 } 157 158 XMLHttpRequestObject.send(data); 159 } 160 } 161 162 function postDataReturnXml(url, data, callback) 163 { 164 var XMLHttpRequestObject = false; 165 166 if (window.XMLHttpRequest) { 167 XMLHttpRequestObject = new XMLHttpRequest(); 168 } else if (window.ActiveXObject) { 169 XMLHttpRequestObject = new 170 ActiveXObject("Microsoft.XMLHTTP"); 171 } 172 173 if(XMLHttpRequestObject) { 174 XMLHttpRequestObject.open("POST", url); 175 XMLHttpRequestObject.setRequestHeader('Content-Type', 176 'application/x-www-form-urlencoded'); 177 178 XMLHttpRequestObject.onreadystatechange = function() 179 { 180 if (XMLHttpRequestObject.readyState == 4 && 181 XMLHttpRequestObject.status == 200) { 182 callback(XMLHttpRequestObject.responseXML); 183 delete XMLHttpRequestObject; 184 XMLHttpRequestObject = null; 185 } 186 } 187 188 XMLHttpRequestObject.send(data); 189 } 190 } 191 192 193