Tuesday, October 21, 2008

A Simple AJAX Engine - JavaScript

Just copy and paste the part in ur java script.

// The AJAX Engine

function AJAXInteraction(url, callback) {
var req = init();
req.onreadystatechange = processRequest;

function init() {
if (window.XMLHttpRequest) {
return new XMLHttpRequest();
} else if (window.ActiveXObject) {
return new ActiveXObject("Microsoft.XMLHTTP");
}
}

function processRequest () {
// readyState of 4 signifies request is complete
if (req.readyState == 4) {
// status of 200 signifies sucessful HTTP call
if (req.status == 200) {
if (callback) callback(req.responseXML);
}
}
}

this.doGet = function() {
req.open("GET", url, true);
req.send(null);
}
}
// End of AJAX Engine
then to send data to the server use a method as follows.
first create an instance of the AXAJIntercation. For that you sholud enter the request URL ann the methord that should be called when the response has come back.
var ajax = new AJAXInteraction(request_url_comes_here, response_method);
ajax.doGet();
after that athe axax.doGet() metord sends the request url in non-blocking way to the server.
and you have to create a response methord as follows
function respond_method(responseXML){
}
when the response has come back to the client, it calls the javaScript methord with passing the responseXML as a variable.

********************************************************************************
This code is copied from somewhere else, i think its not a bad thing to publish this, while it is always visible to the client side. Thank you very much for the person who invented it.
********************************************************************************

No comments: