Download: /howto/webdesign/templates/ajaxrequest.js
//AjaxRequest.js function AjaxRequest( new_url, new_onreceive ) { var self = this; this.url = new_url; this.onreceive; if (new_onreceive) { this.onreceive = new_onreceive; } var http = get_http_object(); function get_http_object() { var http_obj; // Special IE only code /*@cc_on @if (@_jscript_version >= 5) try { http_obj = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { http_obj = new ActiveXObject("Microsoft.XMLHTTP"); } catch { (E) xmlhttp = false; } } @else http_obj = false; @end @*/ // Every other browser on the planet if (!http_obj && typeof XMLHttpRequest != 'undefined') { try { http_obj = new XMLHttpRequest(); } catch (e) { http_obj = false; } } return http_obj; } // Request response this.poll = function() { http.open( 'GET', self.url, true ); http.onreadystatechange = self.receive; http.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' ); http.send( null ); } // Process response this.receive = function() { if (http.readyState != 4) { /* 0: request not initialized 1: server connection established 2: request received 3: processing request 4: request finished and response is ready */ return; } // readyState if (self.onreceive) { // Is there a user defined onreceive method? do_eval = self.onreceive( http ); } else { do_eval = true; } if (do_eval) { eval( http.responseText ); } } // receive // Constructor if (new_onreceive == undefined) { self.poll(); } } // AjaxRequest(); /* Example usage */ new AjaxRequest( 'target.php', function(http) { alert( http.responseText ); }).poll();