Body Onload Event
This code doesn't see开发者_StackOverflowm to work:
document.body.onload = function () { ... }
I know there is a onload
event for the body tag in html, but how come you can't access it from JavaScript? Is window.onload
same as <body onload="...
It basically never triggers.
- There is no
body.onload
. When you attach the event in the HTML code, it actually attaches towindow.onload
. So that's the one you should use. - It only fires when all the resources (images, scripts, css files, etc.) have fully loaded.
- If there is another script which also tries to attach to the event (and almost all javascript frameworks do, like jQuery, ExtJS, etc.), then you might get a conflict there. Better attach to the event through the framework;
- How do you attach it? Maybe you attach the event handler too late, when the page is already loaded.
精彩评论