A couple of days ago I was browsing the net for some remote scripting (also known as AJAX) examples and how to use the XmlHttpRequest object. However, all examples were quite nasty in imiplementation so I decided to write an nice little javascript wrapper around the XmlHttpRequest object.
// JavaScript Document
function EzRemoteScripter() {
//private variables
var _XmlHttpRequest = null;
var _This = null;
Note the usage of the header. It is very important to identify the data as xml so that the XmlHttpRequest object can use the data in order to be manipulated with the standart DOM methods.
Just create a new javascript file with the name test.js (or whatever you want to) and write following:
// JavaScript Document
////////////////////////////////////////////////////////////
var test = new EzRemoteScripter();
var div = window.document.getElementById("DataContainer");
//this function is used to find our div element that will
//show all data that is being returned by our wrapper.
function SetDiv() {
if( ! div) {
div = window.document.getElementById("DataContainer");
}
}
////////////////////////////////////////////////////////////
//here we set the OnUninit event with a function.
//this event is fired if the XmlHttpRequest object was not
//initialized when we call the remote php script.
test.OnUninit = function() {
SetDiv();
div.innerHTML = "Uninitialized!!";
}
////////////////////////////////////////////////////////////
//This event is firing while data is being received from our remote script. Notice that when using document.getElement[..] you have to make sure that the element is set to our variable div.
test.OnLoading = function() {
SetDiv();
div.innerHTML = "Loading...";
}
////////////////////////////////////////////////////////////
//Event is fired when data finished loading
test.OnLoaded = function() {
SetDiv();
div.innerHTML = "Loaded...";
}
////////////////////////////////////////////////////////////
//now this event is kinda tricky. Now when finished loading all data resides in the XmlHttpRequest object.
//however when getting the data from the XmlHttpRequest object the OnInteractive event is fired. That means it is the data transfer between the XmlHttpRequest object and your script.
test.OnInteractive = function() {
SetDiv();
div.innerHTML = "Interacting...";
}
////////////////////////////////////////////////////////////
//this event is the most used and most important event.
//it is fired if the data has been loaded with a success.
//
test.OnSuccess = function() {
SetDiv();
div.innerHTML = "";
// here we are getting the data from the XmlHttpRequest object (OnInteractive is being fired)
//alternatively you may get the data as a string using the test.GetResponseText()
var obj = test.GetResponseXML();
var books = obj.getElementsByTagName("book");
for(var i=0; i < books.length; i++) {
var myLI = document.createElement("LI");
myLI.innerHTML = books[i].firstChild.data;
div.appendChild(myLI);
}
}
////////////////////////////////////////////////////////////
test.OnFailure = function() {
SetDiv();
div.innerHTML = "Failed!";
}
////////////////////////////////////////////////////////////
//this is the funciton that actually calls out php script to get the xml data. pretty easy to use.
//However, you HAVE to init and commit on each call otherwise the script won't work. you may also use the POST method to tranfer data instead of get. To do this you would have to do following:
//d.InitXmlHttpRequest("POST", "test.php");
//d.Commit("myVar=hello&mySecondVar=world");
//
//if you don't want to send any data just pass null to the Commit method. If you leave it empty FireFox will complain.
function Go(d) {
d.InitXmlHttpRequest("GET", "test.php");
d.Commit(null);
}
////////////////////////////////////////////////////////////