Run JavaScript function when the DOM is “ready”?

前端 未结 7 2152
一生所求
一生所求 2020-12-03 10:04

I\'m using a JavaScript upload script that says to run the initialize function as soon as the DOM is ready. I currently have it working just fine with either a call to the f

7条回答
  •  伪装坚强ぢ
    2020-12-03 10:40

    In 2015 you have two options with modern browsers:

    document.onload

    • this fires when the document is loaded, but other resources (most notably images) have not necessarily finished loading.

    window.onload

    • this fires when the document is loaded, AND all other resources (again, most notably images) are loaded.

    Both of the above events would be better utilized with window.addEventListener() of course, as multiple listeners would be allowed.

提交回复
热议问题