giles
giles

Reputation: 11

Does body onload happen before window.onload?

I'm working on the following. It basically passes ?answer=1 if js is enabled. It works until I add the onload argument (as I want this to happen without a user trigger). However adding onload appears to stop (the otherwise working) getElementById argument. Why is this happening?

<script type="text/javascript">
window.onload = function() {
document.getElementById('answer').value = '1';
}
</script>
</head>
<body onload="document.forms[0].submit();">
<form name="form" action="enabled_catch.php" method="get">
<input type="hidden" name="answer">
</form>

thanks

Upvotes: 1

Views: 1004

Answers (1)

Pointy
Pointy

Reputation: 414036

Try this instead:

window.onload = function() {
  document.getElementById("answer").value = '1';
  document.forms[0].submit();
}

As I said in my comment, window.onload is the same thing as the "onload" handler for the <body> tag. You can't have one be one function and the other be another function, therefore, because the "other" isn't really another thing - it's the same thing.

Also, your <input> element needs an "id":

<input type='hidden' name='answer' id='answer'>

Upvotes: 2

Related Questions