Skip navigation.

Why Does UpdateProgress Always Render a DIV?All recent postsAlways Centered AJAX Progress Indicator

Sys.Application.add_load != window.onload

I remember seeing a sample which used Sys.Application.add_load to attach a function you’d normally want to run upon window.onload. It went like this:

function initFoo () {
  // Initialization code here...
}

Sys.Application.add_load (initFoo);

Having browsed the MS AJAX Library source code, I figured the Sys.Application provided a convenient event hook indeed since the class tapped window.load and window.unload for you:

Sys.UI.DomEvent.addHandler (
   window, "unload", this._unloadHandlerDelegate);

Sys.UI.DomEvent.addHandler (
   window, "load", this._loadHandlerDelegate);

To my surprize, initFoo was getting called on every AJAX callback. I figured there was no way get window to fire its onload event several times, so the Sys.Application class must’ve been involved a lot more than I thought.

The remedy is to either hook into window.onload directly:

Sys.UI.DomEvent.addHandler(window, "load", initFoo);

or avoid multiple initializations:

function initFoo () {
  var prm = Sys.WebForms.PageRequestManager.getInstance();

 if (prm.get_isInAsyncPostBack())
     return;

 // Initialization code here...
}

To me, the former looks cleaner and less kludgy than the latter.

Comments

No comments yet

Emails and Notifications

Would you like to be notified when somebody responds to this post? 

TrackBacks

Sorry, TrackBacks are not allowed.

Submit your comment

Please enter only text since all HTML tags except hyperlinks will be stripped. Hyperlinks will become live links. Any comments with flaming or offensive language will be deleted. Be courteous to other posters. Thank you.

Your name (required):
Your email (optional):
Your site's URL (optional):
Enter this number
Type in the number above:
Comment (required):