[removed] vs <body onload=“”/>

后端 未结 13 1792
盖世英雄少女心
盖世英雄少女心 2020-11-22 13:45

What exactly is the difference between the window.onload event and the onload event of the body tag? when do I use which and how shoul

13条回答
  •  遇见更好的自我
    2020-11-22 14:40

    should override window.onload.

    With , document.body.onload might be null, undefined or a function depending on the browser (although getAttribute("onload") should be somewhat consistent for getting the body of the anonymous function as a string). With window.onload, when you assign a function to it, window.onload will be a function consistently across browsers. If that matters to you, use window.onload.

    window.onload is better for separating the JS from your content anyway. There's not much reason to use anyway when you can use window.onload.

    In Opera, the event target for window.onload and (and even window.addEventListener("load", func, false)) will be the window instead of the document like in Safari and Firefox. But, 'this' will be the window across browsers.

    What this means is that, when it matters, you should wrap the crud and make things consistent or use a library that does it for you.

提交回复
热议问题