rubixibuc
rubixibuc

Reputation: 7427

Body Onload Event

This code doesn't seem 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.

Upvotes: 0

Views: 1835

Answers (1)

Vilx-
Vilx-

Reputation: 107052

  1. There is no body.onload. When you attach the event in the HTML code, it actually attaches to window.onload. So that's the one you should use.
  2. It only fires when all the resources (images, scripts, css files, etc.) have fully loaded.
  3. 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;
  4. How do you attach it? Maybe you attach the event handler too late, when the page is already loaded.

Upvotes: 5

Related Questions