Forum Moderators: open

Message Too Old, No Replies

Strange problem with onLoad

         

optik

12:48 pm on Dec 30, 2009 (gmt 0)

10+ Year Member



does anyone know why this doesn't work...

<html>
<head>
<script type="text/javascript" language="JavaScript">

window.onload = setPage('a','b');
function setPage(a,b){
document.getElementById("hide").style.display ="none";

}
</script>
</head>
<body>
<div id="hide">
Hide
</div>
</body>
</html>

yet this does

<html>
<head>
<script type="text/javascript" language="JavaScript">

window.onload = setPage;
function setPage(a,b){
document.getElementById("hide").style.display ="none";

}
</script>
</head>
<body>
<div id="hide">
Hide
</div>
</body>
</html>

daveVk

1:14 pm on Dec 30, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



this

window.onload = setPage('a','b');

executes setPage('a','b') and returns the result, null to onload

It is the same as

setPage('a','b');
window.onload = null;

perhaps you need

window.onload = function(){setPage('a','b');}

Fotiman

1:46 pm on Dec 30, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



window.onload = setPage('a','b');

The parenthesis will cause the function to execute immediately, and the result gets assigned to window.onload. If you need to call a function that takes parameters, then wrap it in an anonymous function:


window.onload = function () {
setPage('a','b');
};

setPage will then only execute when the anonymous function is called (and the anonymous function is assigned to the window.onload).

optik

2:45 pm on Dec 30, 2009 (gmt 0)

10+ Year Member



thanks that solved it