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 should it be done correctly?
标签:
javascript
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
- Can php detect if javascript is on or not?
'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:
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.
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 calledDOMContentLoaded
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.window.onload
- Called after all DOM, JS files, Images, Iframes, Extensions and others completely loaded. This is equal to $(window).load(function() {});body onload=""
- Called once DOM loaded. This is equal to $(document).ready(function() {});window.onload = myOnloadFunc
and<body onload="myOnloadFunc();">
are different ways of using the same event. Usingwindow.onload
is less obtrusive though - it takes your JavaScript out of the HTML.All of the common JavaScript libraries, Prototype, ExtJS, Dojo, JQuery, YUI, etc. provide nice wrappers around events that occur as the document is loaded. You can listen for the window onLoad event, and react to that, but onLoad is not fired until all resources have been downloaded, so your event handler won't be executed until that last huge image has been fetched. In some cases that's exactly what you want, in others you might find that listening for when the DOM is ready is more appropriate - this event is similar to onLoad but fires without waiting for images, etc. to download.
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:or
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.)
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.