views:

1789

answers:

4

I'm familiar with the typical use of onload, as in the following:

<body onload="alert('Hello, World!');">
...
</body>

Is there anywhere else in a page that onload is valid? I can experiment to see if it will work in <script>...</script> tags, but it might save me some time if anyone knows all the places that it's valid to use onload.

+2  A: 

onload is an event specific to the body, frame, iframe, img, link, and script elements. Basically anything which represents a resource to be loaded. For body, that is the document in question. For the others, each is fairly obvious.

Rex M
A: 

According to this page, you can use onload with: <body>, <frame>, <frameset>, <iframe>, <img>, <link>, <script>.

Daniel Lew
+4  A: 

'onload' is supported by the following HTML tags:

<body>, <frame>, <frameset>, <iframe>, <img>, <link>, <script>

And the following Javascript objects:

image, layer, window

Source w3schools

Brian R. Bondy
What about non-tags, like a DOM document or window? (I am unsure, but I think they have onload as well.)
strager
Wow, that update was fast. ;P +1
strager
Thanks. You guys make me look lazy. :)
Bill the Lizard
@strager: I was writing about them as you wrote that comment :)
Brian R. Bondy
@Bill the Lizard: With 1107 answers, I think you can afford to ask as many questions as you want :)
Brian R. Bondy
@Brian: Thanks. I'm just sitting here trying to re-learn JavaScript from a book, and for some dumb reason it never occurred to me to check w3schools before checking SO. :)
Bill the Lizard
You'd think that #Bill the Lizard will naturally be aesthetically familiar with the Rhinoceros book, where this is easy to find. (Javascript the Definitive Guide - O'Reilly, of course.)
le dorfier
Actually, HTML4.01 only allows `onload` in <frameset> and <body> (source: http://www.w3.org/TR/html401/interact/scripts.html#adef-onload ), and I *think* there are some cross-browser issues with `onload` for script tags
Christoph
+1  A: 

Many elements have the onload event. You can find them here

But if you want to test the loading of the DOM, then it's best to use the window.onload. It's also recommended that you separate the javascript code from the HTML markup.

andi