<body onload="something()">

T

tuxedo

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?
 
N

naixn

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 ;)
 
T

tuxedo

naixn said:
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.
 
R

Richard Cornford

tuxedo said:
naixn said:
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.
 
R

Richard Cornford

naixn wrote:
or
window.attachEvent('load', dothis); // For IE !
<snip>

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

Richard.
 
T

tuxedo

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,814
Latest member
SpicetreeDigital

Latest Threads

Top