Forum Moderators: open

Message Too Old, No Replies

cross browser body onload.

         

adamnichols45

3:34 pm on May 28, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Im wanting to have an onload event that is cross browser compatable.

it needs to focus on the text input box.

At the moment I have

<body marginheight="0" topmargin="0" onLoad="document.cart.qty1.focus()">

but this throws up an error in ie although it does still work!

any ideas?

cheers

rocknbil

11:42 pm on May 28, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Idea one: Don't do this. See #1 on the list [webmasterworld.com]. It doesn't work as you'd expect it to and is annoying as can be.

Idea two: Okay, you're resolved to do this, look at window.onload:


window.onload=function() {
if (document.getElementById('qty1')) {
document.getElementById('qty1').focus();
}
};

You're getting an error because only body has loaded, qty1 has not yet loaded. You could also do, right before closing body tag,


<script type="text/javascript">
if (document.getElementById('qty1')) {
document.getElementById('qty1').focus();
}
</script>
</body>
</html>

Don't forget to add ID's to your form elements, which you should be doing anyway for proper labels.

adamnichols45

9:56 pm on Jun 7, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



thanks for that I will implement that first thing!

thanks again.