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

后端 未结 13 1777
盖世英雄少女心
盖世英雄少女心 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:31

    There is no difference, but you should not use either.

    In many browsers, the window.onload event is not triggered until all images have loaded, which is not what you want. Standards based browsers have an event called DOMContentLoaded which fires earlier, but it is not supported by IE (at the time of writing this answer). I'd recommend using a javascript library which supports a cross browser DOMContentLoaded feature, or finding a well written function you can use. jQuery's $(document).ready(), is a good example.

提交回复
热议问题