469,625 Members | 1,591 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,625 developers. It's quick & easy.

<body onload="something()">

The way the <body onload="something()"works ensures that the complete html
document is loaded before something() is executed.

Can the same be achieved when placing the onload call in document somewhere
except within the body tag, or must it always be in the body tag?

For example, if this is placed elsewhere ...

window.onload(something())

.... it will run before the full document has loaded, so that does not work.

Is it possible to refer to the window only when loaded in any other way?

Dec 9 '06 #1
5 2101
tuxedo wrote :
The way the <body onload="something()"works ensures that the complete html
document is loaded before something() is executed.

Can the same be achieved when placing the onload call in document somewhere
except within the body tag, or must it always be in the body tag?

For example, if this is placed elsewhere ...

window.onload(something())

... it will run before the full document has loaded, so that does not work.

Is it possible to refer to the window only when loaded in any other way?
function dothis(e)
{
alert('loaded!');
}

1/
window.onload = dothis;

2/
window.addEventListener('load', dothis, false);
or
window.attachEvent('load', dothis); // For IE !

3/
window.onload = function(e)
{
alert('loaded!');
}

4/
window.addEventListener('load', function(e)
{
alert('loaded!');
}, false);
ir
window.attacheEvent('load', function(e)
{
alert('loaded!');
}); // for IE !

Hope this helps you ;)

--
Naixn
http://fma-fr.net
Dec 9 '06 #2
naixn wrote:
Hope this helps you ;)
[...]

Thank you the function runs after the page has loaded, and it certainly
helps!

As much as I dislike browser specific code, I understand that I should make
attachEvent run only on IE, and that I can safely make any other browser
run addEventListener.

Dec 9 '06 #3
tuxedo wrote:
naixn wrote:
>Hope this helps you ;)

[...]

Thank you the function runs after the page has loaded,
and it certainly helps!

As much as I dislike browser specific code, I understand
that I should make attachEvent run only on IE, and that I
can safely make any other browser run addEventListener.
Neither of those statements are true. The - attachEvent - method is
supported (at least on some objects) by browsers other than IE, such as
Opera versions 7. The - addEventListener - method can only be used on
browsers that support it, which is certainly most of the modern ones but
by no means all. There is also no formal requirement for the window
object to support - addEventListener - as it is specified as a method of
Nodes, and the window object is not a node.

Richard.
Dec 9 '06 #4
naixn wrote:
<snip>
or
window.attachEvent('load', dothis); // For IE !
<snip>

IE will be happier if the first argument is "onload" rather than "load".

Richard.
Dec 9 '06 #5
Richard Cornford wrote:

[...]

Thanks. On second thoughts, I think I'll leave this method out in exchange
of simply moving the function call below the required loaded elements.

Dec 9 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Rupa | last post: by
6 posts views Thread by Mason A. Clark | last post: by
16 posts views Thread by rik | last post: by
1 post views Thread by =?Utf-8?B?Sm9obg==?= | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.