英文:
Is the Window.load event fired after all non-postponed javaScripts have finished execution?
问题
Window.load event在所有“非延迟执行的JavaScript”执行完成后触发吗?“非延迟执行的JavaScript”被定义为HTML页面加载的任何JavaScript(内联或外部或异步或动态生成的或模块化的),除了以下情况:
- 在onload()事件处理程序内的代码。
- 在等待用户输入的任何其他处理程序内的代码。
规范只是简单地说明load事件在“文档加载完成后”触发。它并没有说明事件是在所有非延迟执行的JavaScript执行完成之前还是之后触发。
以下帖子与此问题相关,但没有直接回答这个问题。
谢谢!
相关帖子:
英文:
Is the Window.load event fired after all "non-postponed JavaScripts" have finished execution? "Non-postponed Javascripts" are defined as any JavaScripts loaded by the HTML page (inline or external or async or dynamically-generated or module), except for:
- Code inside onload() event handler
- Code inside any other handlers that await user inputs
The spec simply said that the load event fires "when the document has finished loading". It does not say whether or not the event fires before or after all non-postponed JavaScripts have finished execution.
The following posts are related, but do not answer this question directly.
Thanks!
Related Posts:
答案1
得分: 2
WHATWG 维护 HTML Living Standard。因此,如果他们说 "load" 事件在 "文档完成加载" 时触发,那么浏览器遵循这种行为。脚本不一定在 "onload" 之前完成。
英文:
WHATWG maintains the HTML Living Standard. Therefore, if they say that the "load" event is triggered "when the document has finished loading, " then that is the behaviour that browsers follow. The scripts do not necessarily finish before "onload".
https://html.spec.whatwg.org/multipage/indices.html#event-load
答案2
得分: 0
window.onload
等待所有外部资源和同步的 JavaScript 加载,但不等待:
- 异步脚本
<script async src="..." />
。 - 延迟脚本
<script defer src="..." />
。 - 模块
<script type="module" src="..." />
。 - 事件处理程序代码,例如:
onload()
,onclick()
。直到事件发生时才执行。
英文:
window.onload
waits for all external resources and synchronous Javascript to load, but it doesn't wait for:
- Asynchronous scripts
<script async src="...
. - Defered scripts
<script defer src="...
. - Modules
<script type="module" src="...
. - Event handler code eg.:
onload()
,onclick()
. this not executed until the event happens.
Resources:
https://flaviocopes.com/javascript-async-defer/
https://html.spec.whatwg.org/multipage/scripting.html#the-script-element
https://developer.mozilla.org/en-US/docs/Web/API/Window/load_event
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论