ASP.NET AJAX pageLoad() 和 JavaScript window.onload 之间有什么区别? [英] What's the different between ASP.NET AJAX pageLoad() and JavaScript window.onload?
问题描述
我正在使用 ASP.NET AJAX 并想了解这两个代码段之间的区别:
I'm working with ASP.NET AJAX and want to understand the difference between these two snippets:
function pageLoad(sender, eventArgs) { }
和
window.onload = function() { }
- 他们的行为一样吗?
- 或者一个在另一个之前调用?
- 还是会自动调用一个而另一个不调用?
Sys.Application.initialize()
开始生命周期的init
部分.这initialize()
是所有客户端 AJAX 控件,之后它们就可以以编程方式进行交互了Sys.Application
开始生命周期的load
部分,调用所有订阅此事件的处理程序- 最后调用全局函数
pageLoad
(如果定义了) Sys.Application.initialize()
begins theinit
part of the life cycle. Thisinitialize()
s all clientside AJAX controls, after which they're ready to be interacted with programaticallySys.Application
begins theload
part of the life cycle, calling all handlers that have subscribed to this event- Finally, it calls the global function
pageLoad
(if one is defined)
推荐答案
首先要注意几点.MS 发明了一种名为 Sys. 的客户端运行时对象".应用
.它在页面的 [客户端] 生命周期内处理引发 init
、load
和 unload
事件,如下所示:
A couple things to note first. MS invented a sort of "clientside runtime object" called Sys.Application
. It handles raising init
, load
, and unload
events throughout the [clientside] lifespan of the page, as follows:
对每个部分(即 AJAX + UpdatePanel)回发重复步骤 2) 和 3).
Step 2) and 3) are repeated for every partial (ie AJAX + UpdatePanel) postback.
最后的答案:pageLoad
只是Sys.Application.add_load()
.
然而,关于它与 window.onload
的关系,事情开始变得有趣起来.本质上,MS 需要 window.onload
仅在 init
阶段完成后 触发.但是您无法控制浏览器何时触发 onload
,因为它与内容加载" 相关联.这被称为window.onload
问题"一个>:
With regards to its relationship to window.onload
however, things start to get interesting. Essentially, MS needed window.onload
to fire only after the init
phase was complete. But you can't control when the browser will fire onload
, as it's tied to "content loaded". This is known as "the window.onload
problem":
onload 事件在所有页面之后触发内容已加载(包括图像和其他二进制内容).如果你的页面包含大量图像,然后您可能会看到明显的滞后页面变为活动状态.
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.
因此,他们刚刚发明了自己的特殊"函数,以在其事件生命周期的正确时间触发,并将其称为 "pageLoad"
.他们用来启动这个自定义事件生命周期的技巧是放置在结束 </form>
标记之前调用 Sys.Application.initialize()
.服务器端运行时执行此操作.精明的读者会注意到这个技巧允许 MS 解决 window.onload
问题,因为你放入 pageLoad
的任何代码都会独立于二进制内容(w/IE 的一个罕见问题).
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 to Sys.Application.initialize()
just before the closing </form>
tag. The serverside runtime does this. 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).
>他们的行为相同吗?
概念上是的,实际上完全不是由于上述 window.onload
问题.唯一的规则是您应该将与 AJAX 控件交互的代码仅放在 pageLoad
中,因为 window.onload
遵循其自己的事件轨迹.
Conceptually yes, in practice not at all due to said window.onload
problem. 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.
>还是一个在另一个之前调用?
他们是完全、100% 独立的.
They are completely, 100% independent.
>还是会自动调用一个而另一个不调用?
如果你定义了它们,它们都会被调用.
They will both be called if you have them defined.
这篇关于ASP.NET AJAX pageLoad() 和 JavaScript window.onload 之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!