简体   繁体   English

window.onload函数如何工作?

[英]How does the window.onload function work?

I don't know javascript well. 我不太了解javascript。 So may be stupid question here: 所以这里可能是个愚蠢的问题:

Suppose, we can add a prototype or say property to the window object like 假设,我们可以在窗口对象中添加原型或者说属性

window.something = function(){..}

or, 要么,

window.something = "somestring"

But how window.onload = function(){....} is not creating onload function but assigning it to run when loaded.... 但是window.onload = function(){....}是如何创建onload函数但是在加载时指定它运行的....

And if the onload is built-in function then window.onload = function(){...} should override the onload function....?!?! 如果onload是内置函数,那么window.onload = function(){...}应该覆盖onload函数....?!?!

That's called by the browser ! 这是由浏览器调用的!

window.onload gets fired after the main HTML, all CSS, all images and all other resources have been loaded and rendered. 在主HTML,所有CSS,所有图像和所有其他资源都已加载和呈现后,window.onload被触发。

The window.onload property is created by the browser and exists by default. window.onload属性由浏览器创建,默认存在。 By default is has a value of null . 默认情况下,其值为null But, if you assign a function to it (so it contains a valid function instead of null ), then the browser will call that function when the page resources have finished loading. 但是,如果你为它分配一个函数(所以它包含一个有效的函数而不是null ),那么浏览器将在页面资源加载完成后调用该函数。

Here's a little demo that illustrates this: http://jsfiddle.net/jfriend00/7z48j/ 这是一个小演示,说明了这一点: http//jsfiddle.net/jfriend00/7z48j/

Many properties of the window and document object are recognized specially by the browser. 浏览器特别识别windowdocument对象的许多属性。 In this case, when the window.onload property is set, the browser automatically runs the function after loading the document into the browser window. 在这种情况下,当设置window.onload属性时,浏览器会在将文档加载到浏览器窗口后自动运行该功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM