/ Published in: JavaScript
Public domain. To create an AJAX object: myAJAXObject = new ajax;. To submit and/or retrieve data: myAJAXObject.sendRequest('GET', 'search_results.php', false, 'query=test&per_page=9', 'divSearchResults');. Updated 1/28/11 to fix bug preventing POST data from being sent.
Expand |
Embed | Plain Text
/* * AJAX Library * Created November 26, 2010 * Updated January 28, 2011 * Released into the public domain by Josh Atkins */ function ajax() { this.ajaxObject = new XMLHttpRequest() || new ActiveXObject('Microsoft.XMLHTTP'); var replace_html = false; this.ajaxObject.onreadystatechange = function() { if(this.readyState==4&&this.status==200) { if(replace_html) { if(document.getElementById(replace_html).value) document.getElementById(replace_html).value = this.responseText; else document.getElementById(replace_html).innerHTML = this.responseText; } } }; ajax.prototype.sendRequest = function(method, uri, async, data, replace_html_element) { replace_html = replace_html_element; this.ajaxObject.open(method.toUpperCase(), uri + (method.toLowerCase() == 'get' ? '?' + data : ''), async); this.ajaxObject.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded') this.ajaxObject.send(method.toLowerCase() == 'get' ? '' : data); }; }
Comments
Subscribe to comments
You need to login to post a comment.

Hey, man, nice job! Just a little issue about calling: "myAJAXObject.send('GET', 'searchresults.php', false, 'query=test&perpage=9', 'divSearchResults');"
The method stands for 'sendRequest()' instead of 'send()'.
However, a very tiny and useful code. Gratz.
Thanks for pointing that out. I've fixed it now. The code is basically based off this from W3Schools, but it's more flexible and some of the code is redone in a more compact form.