I'm working with ASP.NET AJAX and want to understand the difference between these two snippets:
function pageLoad(sender, eventArgs) { }
and
window.onload = function() { }
- Do they act the same?
- Or is one called before the other?
- Or will one be called automatically and the another not?
-
A couple things to note first. MS invented a sort of "clientside runtime object" called
Sys.Application. It handles raisinginit,load, andunloadevents throughout the [clientside] lifespan of the page, as follows:Sys.Application.initialize()begins theinitpart of the life cycle. Thisinitialize()s all clientside AJAX controls, after which they're ready to be interacted with programaticallySys.Applicationbegins theloadpart of the life cycle, calling all handlers that have subscribed to this event- Finally, it calls the global function
pageLoad(if one is defined)
Step 2) and 3) are repeated for every partial (ie AJAX + UpdatePanel) postback.
So finally the answer:
pageLoadis just a handy shortcut toSys.Application.add_load().With regards to its relationship to
window.onloadhowever, things start to get interesting. Essentially, MS neededwindow.onloadto fire only after theinitphase was complete. But you can't control when the browser will fireonload, as it's tied to "content loaded". This is known as "thewindow.onloadproblem":onload event fires after all page content has loaded (including images and other binary content). If your page includes lots of images then you may see a noticeable lag before the page becomes active.
So, they just invented their own "special" function to fire at just the right time in their event life cycle and called it
"pageLoad". And the trick that they used to kickoff this custom event life cycle was to place the call toSys.Application.initialize()just before the closing</form>tag. The serverside runtime does this. Astute readers will note that this trick allowed MS to solve thewindow.onloadproblem, since any code you put intopageLoadwill fire independent of binary content (w/ one rare catch for IE).> Do they act the same?
Conceptually yes, in practice not at all due to said
window.onloadproblem. The only rule is that you should put code that interacts with your AJAX controls inpageLoadonly, sincewindow.onloadfollows its own event trajectory.> Or is one called before the other?
They are completely, 100% independent.
> Or will one be called automatically and the another not?
They will both be called if you have them defined.
Amr ElGarhy : your answer is complete, but i have another issue with pageLoad, when i put in my page i found that it keep called and called automatically, note: i have telerik:RadAjaxManager on the page, so this is because of an error happening or its normal to keep called like that "as its called by a timer"?annakata : nice write up +1 - the solution is so very laughably microsoftCrescent Fresh : @amrelgarhy: see my updated answer: Step 2) and 3) are called on every partial (ie AJAX + UpdatePanel) postback.Tomalak : @crescentfresh: +1. Why did you delete your other answer? It's more or less the same. *looks confused*
0 comments:
Post a Comment