Click here to Skip to main content
15,884,473 members
Articles / Web Development / HTML

AjaxDelegate - An AJAX library with easy-to-use callback functionality

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
3 Apr 20063 min read 52.6K   247   41  
An AJAX library with easy-to-use callback functionality.
<!--
/*
AjaxDelegate(url, callback)
	url = the url of the page that will do the server-side processing
	callback = the name of the function to call once the call has completed
	
	Any number of additional arguments can also be specified. The extra 
	arguments will be available to the callback function when it is called.
	
	The callback function will receive the following arguments:
		callback(url, response, [argument[0]], etc.)
	where:
		url = the url of the page that did the server-side processing
		response = the actual HTTP responseText returned from the call
		[argument[0]], etc. = the remaining arguments that were originally passed to the AjaxDelegate constructor
*/
function AjaxDelegate(url, callback)
{
	// basic properties
	this.url = url;
	this.callback = callback;
	this.callbackArguments = arguments;

	// methods
	this.Fetch = ajaxFetch;

	// XmlHttpRequest object
	this.request = null;
}

/*
ajaxFetch()
	Asynchronously calls the url specified in the AjaxDelegate constructor.
	When the call completes, the callback specified in the AjaxDelegate constructor
	is called, passing the responseText data in.
*/
function ajaxFetch()
{
	// this gets the variables into a local scope
    var request = this.request;
    var callback = this.callback;
    var callbackArguments = this.callbackArguments;

    // branch for native XMLHttpRequest object
    if(window.XMLHttpRequest) {
    	try {
			request = new XMLHttpRequest();
        } catch(e) {
			request = null;
        }
    // branch for IE/Windows ActiveX version
    } else if(window.ActiveXObject) {
       	try {
        	request = new ActiveXObject("Msxml2.XMLHTTP");
      	} catch(e) {
        	try {
          		request = new ActiveXObject("Microsoft.XMLHTTP");
        	} catch(e) {
          		request = null;
        	}
		}
    }

	// if we were able to create the XmlHTTPRequest object, we can make the request
	if(request) {
		request.onreadystatechange = function () {
										if(request.readyState == 4) {
											if(request.status == 200) {
												// we replace the second argument (callback function) with the response data)
												// kind of cheesy, but it is nice and easy and works well
												callbackArguments[1] = request.responseText;
												callback.apply(this, callbackArguments);
											} else {
												alert("There was a problem retrieving the XML data:\n" + request.statusText);
											}

											// clean up
											request = null;
										}
									}
		request.open("GET", this.url, true);
		request.send("");
	}
}
//-->

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions