Window.load事件在所有未被延迟执行的JavaScript完成后触发吗?

huangapple go评论68阅读模式
英文:

Is the Window.load event fired after all non-postponed javaScripts have finished execution?

问题

Window.load event在所有“非延迟执行的JavaScript”执行完成后触发吗?“非延迟执行的JavaScript”被定义为HTML页面加载的任何JavaScript(内联或外部或异步或动态生成的或模块化的),除了以下情况:

  1. 在onload()事件处理程序内的代码。
  2. 在等待用户输入的任何其他处理程序内的代码。

规范只是简单地说明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:

  1. Code inside onload() event handler
  2. 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!

答案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 加载,但不等待:

  1. 异步脚本 <script async src="..." />
  2. 延迟脚本 <script defer src="..." />
  3. 模块 <script type="module" src="..." />
  4. 事件处理程序代码,例如:onload()onclick()。直到事件发生时才执行。
英文:

window.onload waits for all external resources and synchronous Javascript to load, but it doesn't wait for:

  1. Asynchronous scripts &lt;script async src=&quot;....
  2. Defered scripts &lt;script defer src=&quot;....
  3. Modules &lt;script type=&quot;module&quot; src=&quot;....
  4. 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

huangapple
  • 本文由 发表于 2023年6月2日 11:10:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76386902.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定