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. If alert on your page appears before your page then return false from js function. For more information click here.
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. If alert on your page appears before your page then return false from js function. For more information click here.
Comments