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

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

    They both work the same. However, note that if both are defined, only one of them will be invoked. I generally avoid using either of them directly. Instead, you can attach an event handler to the load event. This way you can incorporate more easily other JS packages which might also need to attach a callback to the onload event.

    Any JS framework will have cross-browser methods for event handlers.

    0 讨论(0)
  • 2020-11-22 14:37

    'so many subjective answers to an objective question. "Unobtrusive" JavaScript is superstition like the old rule to never use gotos. Write code in a way that helps you reliably accomplish your goal, not according to someone's trendy religious beliefs.

    Anyone who finds:

     <body onload="body_onload();">
    

    to be overly distracting is overly pretentious and doesn't have their priorities straight.

    I normally put my JavaScript code in a separate .js file, but I find nothing cumbersome about hooking event handlers in HTML, which is valid HTML by the way.

    0 讨论(0)
  • 2020-11-22 14:40

    window.onload can work without body. Create page with only the script tags and open it in a browser. The page doesn't contain any body, but it still works..

    <script>
      function testSp()
      {
        alert("hit");
      }
      window.onload=testSp;
    </script>
    
    0 讨论(0)
  • 2020-11-22 14:40

    <body onload=""> should override window.onload.

    With <body onload="">, 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 <body onload=""> anyway when you can use window.onload.

    In Opera, the event target for window.onload and <body onload=""> (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.

    0 讨论(0)
  • 2020-11-22 14:41

    I prefer, generally, to not use the <body onload=""> 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 <head> 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.)

    0 讨论(0)
  • 2020-11-22 14:44

    It is a accepted standard to have content, layout and behavior separate. So window.onload() will be more suitable to use than <body onload=""> though both do the same work.

    0 讨论(0)
提交回复
热议问题