Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
157 views
in Technique[技术] by (71.8m points)

javascript - window.onload vs document.onload [关闭](window.onload vs document.onload [closed])

哪个被更广泛支持: window.onloaddocument.onload

  ask by Chris Ballance translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

When do they fire? (他们什么时候开火?)

window.onload

  • By default, it is fired when the entire page loads, including its content (images, CSS, scripts, etc.). (默认情况下,在整个页面加载时会触发它, 包括其内容(图像,CSS,脚本等)。)

In some browsers it now takes over the role of document.onload and fires when the DOM is ready as well. (在某些浏览器中,它现在接管了document.onload的角色,并在DOM准备好时触发。)

document.onload

  • It is called when the DOM is ready which can be prior to images and other external content is loaded. (当DOM准备就绪时调用它,它可以图像之前加载其他外部内容。)

How well are they supported? (他们的支持程度如何?)

window.onload appears to be the most widely supported. (window.onload似乎是最广泛支持的。) In fact, some of the most modern browsers have in a sense replaced document.onload with window.onload . (实际上,一些最现代的浏览器在某种意义上用window.onload替换了document.onload 。)

Browser support issues are most likely the reason why many people are starting to use libraries such as jQuery to handle the checking for the document being ready, like so: (浏览器支持问题很可能是许多人开始使用诸如jQuery之类的库来处理文档准备就绪的原因,如下所示:)

$(document).ready(function() { /* code here */ });
$(function() { /* code here */ });

For the purpose of history. (出于历史的目的。) window.onload vs body.onload : (window.onload vs body.onload :)

A similar question was asked on codingforums a while back regarding the usage of window.onload over body.onload . (关于window.onloadbody.onload的使用情况,有人在编码论坛提出了类似的问题。) The result seemed to be that you should use window.onload because it is good to separate your structure from the action. (结果似乎是你应该使用window.onload因为将结构与动作分开是很好的。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...