什么是ASP.NET AJAX页面加载()和JavaScript的window.onload之间有什么不同?有什么不同、加载、页面、AJAX

2023-09-02 11:56:28 作者:╰冷月残花べ

我正在与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() { }

请他们的行为一样吗? 或者是前一个其他的叫什么? 或者将来有自动调用,另一个不?

推荐答案

一对夫妇的事情,首先要注意。 MS发明了一种客户方运行时对象的名为Sys.Application.它可以处理整个募集的init 负荷卸载事件在[客户方]寿命的网页,如下:

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:

Sys.Application.initialize()开始生命周期的的init 部分。这初始化()一切都客户方AJAX控件,之后,他们就可以用编程方式进行交互 Sys.Application 开始负荷的生命周期的一部分,称已经订阅了此事件的所有处理程序 最后,它调用全局函数页面加载(如果定义) Sys.Application.initialize() begins the init part of the life cycle. This initialize()s all clientside AJAX controls, after which they're ready to be interacted with programatically Sys.Application begins the load 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)

步骤2)和3)被重复用于每一部分(即AJAX +的UpdatePanel)回发

Step 2) and 3) are repeated for every partial (ie AJAX + UpdatePanel) postback.

所以最后的答案:页面加载只是一个方便的快捷方式Sys.Application.add_load().

So finally the answer: pageLoad is just a handy shortcut to 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.

于是,他们就发明了自己的特殊功能,以火在适当的时候在他们的活动的生命周期,并把它称为页面加载。而他们用来开球此自定义事件生命周期的诀窍是发出呼叫到 Sys.Application.initialize()之前结束&LT; /形式GT; 标记。在服务器端运行时执行此操作。细心的读者会注意到,这一招使得MS解决的window.onload 的问题,因为任何code,你把页面加载将会触发独立的二进制内容(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).

&GT;难道他们的行为是否相同?

在概念上是的,实际上一点都没有因为说的window.onload 的问题。唯一的规则是,你应该把code,以你的AJAX控件在页面加载只,因为的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.

&GT;抑或是前一个其他的叫什么?

他们是完全,100%自主。

They are completely, 100% independent.

&GT;或者会不会有被自动调用和另一个不?

他们都将被称为如果你有他们定义的。

They will both be called if you have them defined.