[英]What's the different between ASP.NET AJAX pageLoad() and JavaScript window.onload?
I'm working with ASP.NET AJAX and want to understand the difference between these two snippets: 我正在使用ASP.NET AJAX,并希望了解这两个片段之间的区别:
function pageLoad(sender, eventArgs) { }
and 和
window.onload = function() { }
A couple things to note first. 首先要注意的几件事情。 MS invented a sort of "clientside runtime object" called
Sys.Application
. MS发明了一种名为
Sys.Application
的“客户端运行时对象”。 It handles raising init
, load
, and unload
events throughout the [clientside] lifespan of the page, as follows: 它处理在页面的[clientside]生命周期内提升
init
, load
和unload
事件,如下所示:
Sys.Application.initialize()
begins the init
part of the life cycle. Sys.Application.initialize()
开始生命周期的init
部分。 This initialize()
s all clientside AJAX controls, after which they're ready to be interacted with programatically initialize()
是所有客户端的AJAX控件,之后他们准备以编程方式进行交互 Sys.Application
begins the load
part of the life cycle, calling all handlers that have subscribed to this event Sys.Application
开始生命周期的load
部分,调用已订阅此事件的所有处理程序 pageLoad
(if one is defined) pageLoad
(如果定义了一个) Step 2) and 3) are repeated for every partial (ie AJAX + UpdatePanel) postback. 对于每个部分(即AJAX + UpdatePanel)回发,重复步骤2)和3)。
So finally the answer : pageLoad
is just a handy shortcut to Sys.Application.add_load()
. 最后答案是 :
pageLoad
只是Sys.Application.add_load()
一个方便的快捷方式。
With regards to its relationship to window.onload
however, things start to get interesting. 然而,关于它与
window.onload
关系,事情开始变得有趣。 Essentially, MS needed window.onload
to fire only after the init
phase was complete. 从本质上讲,MS只需要在
init
阶段完成后才能触发window.onload
。 But you can't control when the browser will fire onload
, as it's tied to "content loaded" . 但是你无法控制浏览器何时触发
onload
,因为它与“内容加载”相关联 。 This is known as "the window.onload
problem" : 这被称为“
window.onload
问题” :
onload event fires after all page content has loaded (including images and other binary content).
加载所有页面内容(包括图像和其他二进制内容)后,onload事件将触发。 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"
. 因此,他们刚刚发明了自己的“特殊”功能,可以在事件生命周期的正确时间触发,并称之为
"pageLoad"
。 And the trick that they used to kickoff this custom event life cycle was to place the call to Sys.Application.initialize()
just before the closing </form>
tag . 他们用来启动这个自定义事件生命周期的技巧是在结束
</form>
标记之前调用Sys.Application.initialize()
。 The serverside runtime does this. serverside运行时执行此操作。 Astute readers will note that this trick allowed MS to solve the
window.onload
problem, since any code you put into pageLoad
will fire independent of binary content ( w/ one rare catch for IE ). 精明的读者会注意到这个技巧允许MS解决
window.onload
问题,因为你放入pageLoad
任何代码都将独立于二进制内容( 具有IE的一个罕见捕获 )。
> Do they act the same? >他们的行为是一样的吗?
Conceptually yes, in practice not at all due to said window.onload
problem. 概念上是的,实际上根本没有因为
window.onload
问题。 The only rule is that you should put code that interacts with your AJAX controls in pageLoad
only, since window.onload
follows its own event trajectory. 唯一的规则是你应该只在
pageLoad
放置与你的AJAX控件交互的代码,因为window.onload
遵循它自己的事件轨迹。
> Or is one called before the other? >或者是在另一个之前打电话?
They are completely, 100% independent. 它们完全是100%独立的。
> Or will one be called automatically and the another not? >或者会自动调用一个而另一个不会调用吗?
They will both be called if you have them defined. 如果你定义了它们,它们都会被调用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.