October 28, 2016
Hot Topics:

Ajax and the Yahoo! Connection Manager

  • April 21, 2006
  • By Nicholas Zakas
  • Send Email »
  • More Articles »

Advanced Callback Objects

The callback object isn't limited to just two methods, there are a couple other properties provided for ease of use. The first is the argument property, which you can use to pass an argument into the request so it will be returned in the response object. Note that this value is never sent to the server, it is only used for passing data around on the client. You can specify an type of value you want for this property:

var callback = {
    success: function (oResponse) {
        //retrieve the argument
        var sArg = oResponse.argument;

    failure: function (oResponse) {

        //retrieve the argument
        var sArg = oResponse.argument;


    argument: "string of info"

Once the argument property is defined, it will be returned on the response object that is passed into both the success() and failure() methods. The argument property on the response object contains this value.

There may be a case when you want the success() and failure() methods to call methods on another object. To ease this case, you can use the scope property to set the object scope of methods. For instance, suppose you have an object oObject that has the methods handleSuccess() and handleFailure() that you want to use for success() and failure(), respectively. If you were just to pass in pointers to those methods, you would lose the scope in which they are to be executed (i.e., the this object inside of these methods would not refer to oObject). But, by setting the scope property, you can maintain the proper scope for these methods:

var callback = {
    success: oObject.handleSuccess,
    failure: oObject.handleFailure,
    scope: oObject

Monitoring and Managing Requests

One of the limitations of XMLHttp is the lack of a built-in method to monitor and manage multiple requests. The Yahoo! Connection Manager has implemented features that allow you to determine if a request is still pending as well as the ability to abort a request that has not yet completed.

The asyncRequest() method actually returns an object representing the request that was just made. This object can be used later to determine if the request is still pending by passing it to the isCallInProgress() method, like so:

var oConnect = YAHOO.util.Connect.asyncRequest("get", 
	"info.htm", oCallback, null);
	//outputs "true"

Using this method, you can determine if a particular request has been completed or not. There may even be a time when you have decided that even though the call hasn't completed, you don't want to continue the request. At that time, you can use the abort() method, passing in the same object:

var oConnect = 
	"info.htm", oCallback, null);
if(YAHOO.util.Connect.isCallInProgress(oConnect)) {

Calling abort() stops the current request and frees the resources associated with it.

Page 2 of 3

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Thanks for your registration, follow us on our social networks to keep up-to-date
Rocket Fuel