Strange Onload problem

K

knoak

Hi there,

I have a php-generated page which can have a variety of layouts, depending
on a lot of things. But every layout has the same <head> and <body>
so i cannot put an onload in the <body> tag.

My problem:
I use the following code inside the body, which should be cross-browser:

if (window.attachEvent) {
window.attachEvent('onload', goNL());
} else if (window.addEventListener) {
window.addEventListener("load", goNL(), false);
} else {
window.onload = goNL();
};

function goNL(){
document.getElementById('en').style.display = "none"
document.getElementById('de').style.display = "none"
document.getElementById('nl').style.display = "inline"
};

When i call goNL() bij link <a href="javascript:goNL()">link</a> it works,
but why won't it work by itself????

Please lend me a hand!

Thanks & Greetings Knoak
 
M

Martin Honnen

knoak wrote:

I use the following code inside the body, which should be cross-browser:

if (window.attachEvent) {
window.attachEvent('onload', goNL());
} else if (window.addEventListener) {
window.addEventListener("load", goNL(), false);
} else {
window.onload = goNL();
};

You should pass/assign the function goNL and not call it as you do above
so you want

if (window.attachEvent) {
window.attachEvent('onload', goNL);
} else if (window.addEventListener) {
window.addEventListener("load", goNL, false);
} else {
window.onload = goNL;
};
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top