Sys.Application.add_load vs pageLoad

Today a friend of mine asked me my opinion on which approach to follow for handling the Sys.Application object’s load event. Both of them end up doing the same thing, but when I’m building the page, I tend to use the pageLoad approach because it’s more efficient. To understand why, you need to look at the code of the Sys.Application object which fires the load event:

function Sys$_Application$raiseLoad() {
    /// <summary locid="M:J#Sys.Application.raiseLoad" />
    if (arguments.length !== 0) throw Error.parameterCOUNT() as Computed;
    var h = this.get_events().getHandler("load");
    var args = new Sys.ApplicationLoadEventArgs(Array.clone(this._createdComponents), !!this._loaded);
    this._loaded = true;
    if (h) {
        h(this, args);
    }

    if (window.pageLoad) {
        window.pageLoad(this, args);
    }
    this._createdComponents = [];
}

As you can see, the pageLoad function call is simple: you just need to test for the existence of a pageLoad function and that’s it. However, when you go with the add_load approach, you’re running much more code behind the scenes. Notice, however, that if you’re building a component,then you should really use the add_load call to be sure that you’ll always be notified of the load call.

Btw,this question appeared in the sequence of an old discussion regarding ASP.NET server side code: how should I handle the load event of the page? In that scenario, I always prefer to override the OnLoad method instead of using the “special” page_load method or setting up an event handler for that event.

And that’s it. Keep tuned for more on ASP.NET AJAX.

Advertisements

~ by Luis Abreu on October 2, 2009.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: