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

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

    I prefer, generally, to not use the > event. I think it's cleaner to keep behavior separated from content as much as possible.

    That said, there are occasions (usually pretty rare for me) where using body onload can give a slight speed boost.

    I like to use Prototype so I generally put something like this in the > of my page:

    document.observe("dom:loaded", function(){
      alert('The DOM is loaded!');
    });
    

    or

    Event.observe(window, 'load', function(){
      alert('Window onload');
    });
    

    The above are tricks I learned here. I'm very fond of the concept of attach event handlers outside of the HTML.

    (Edit to correct spelling mistake in code.)

提交回复
热议问题