XMLHttpRequest pure JS ajax
by Riderman Sousa
HTML
<div id="message"></div>
JavaScript
function ajaxRequest(theURL, callbackFunction) {
var thisRequestObject;
thisRequestObject = initiateRequest();
thisRequestObject.onreadystatechange = processRequest;
function initiateRequest() {
if (window.XMLHttpRequest) return new XMLHttpRequest();
elseif(window.ActiveXObject)
return new ActiveXObject("Microsoft.XMLHTTP");
}
function processRequest() {
if (thisRequestObject.readyState == 4) {
if (thisRequestObject.status == 200) {
if (callbackFunction) callbackFunction(thisRequestObject);
}
else alert("There was an error: (" + thisRequestObject.status + ") " + thisRequestObject.statusText);
}
}
this.sendGetData = function() {
if (theURL) {
thisRequestObject.open("GET", theURL, true);
thisRequestObject.send();
}
}
}
$(window).ready(function(){
var ajax new ajaxRequest("http://letsgofestas.com.br/", function(data){ console.log(data) });
ajax.sendGetData()
})