When to use “[removed]”?

前端 未结 6 1920
终归单人心
终归单人心 2020-12-05 00:03

In JavaScript, when I want to run a script once when the page has loaded, should I use window.onload or just write the script?

For example, if I want to

6条回答
  •  旧时难觅i
    2020-12-05 00:21

    You have three alternatives:

    1. Directly inside the script tag runs it as soon as it is parsed.

    2. Inside document.addEventListener( "DOMContentLoaded", function(){}); will run it once the DOM is ready.

    3. Inside window.onload function(){}) will run as soon as all page resources are loaded.

提交回复
热议问题