By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,288 Members | 1,695 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,288 IT Pros & Developers. It's quick & easy.

Can I set focus to a control in the BODY ONLOAD event?

P: n/a
I'd like the focus to be on "mycontrol" when the page is first loaded. I'm
getting an undefined error and am assuming that I can't do this in the body
ONLOAD event because the control is not yet rendered. (but I thought this
would still work)

<body onload="mycontrol.focus()">

Also, is there a way to not produce an error if that control does not exist?

Thanks!
Mike

Jul 20 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Ivo
"Mike Hnatt" <do**@gladstone-inc.com> wrote in message
news:vo************@corp.supernews.com...
I'd like the focus to be on "mycontrol" when the page is first loaded. I'm getting an undefined error and am assuming that I can't do this in the body ONLOAD event because the control is not yet rendered.
The onload event is triggered when everything is rendered, put in the
appropriate places of the DOM etc.
<body onload="mycontrol.focus()">

Also, is there a way to not produce an error if that control does not exist?
Thanks!
Mike


Depends a bit on what your "mycontrol" actually is. Form elements generally
take focus but other elements don't. Also, the way you reference the item in
the onload statement assumes a javascript object (which may point to a html
element) rather than just the id of a tag.
To avoid errors, it is always a good idea to check for the existance of all
variables and functions first, for example:
if ( mycontrol && mycontrol.focus ) mycontrol.focus();
Ivo
Jul 20 '05 #2

P: n/a
Lee
Mike Hnatt said:

I'd like the focus to be on "mycontrol" when the page is first loaded. I'm
getting an undefined error and am assuming that I can't do this in the body
ONLOAD event because the control is not yet rendered. (but I thought this
would still work)

<body onload="mycontrol.focus()">

Also, is there a way to not produce an error if that control does not exist?


Unless you're creating "mycontrol" dynamically, it exists before
the onLoad event will fire. The problem is that you're not
referring to it correctly.

If mycontrol is in a form named "myform", you can refer to it as:

document.myform.mycontrol.focus();

If "mycontrol" is the id attribute of the element, and your target
is reasonably modern browsers, it's better to use:

document.getElementById("mycontrol").focus();

Jul 20 '05 #3

P: n/a
Thanks Lee, it worked! Also, thanks to Ivo for the idea to check if the
control exist. Thanks again,
Mike

"Lee" <RE**************@cox.net> wrote in message
news:bm*********@drn.newsguy.com...
Mike Hnatt said:

I'd like the focus to be on "mycontrol" when the page is first loaded. I'mgetting an undefined error and am assuming that I can't do this in the bodyONLOAD event because the control is not yet rendered. (but I thought thiswould still work)

<body onload="mycontrol.focus()">

Also, is there a way to not produce an error if that control does not
exist?
Unless you're creating "mycontrol" dynamically, it exists before
the onLoad event will fire. The problem is that you're not
referring to it correctly.

If mycontrol is in a form named "myform", you can refer to it as:

document.myform.mycontrol.focus();

If "mycontrol" is the id attribute of the element, and your target
is reasonably modern browsers, it's better to use:

document.getElementById("mycontrol").focus();

Jul 20 '05 #4

P: n/a
Mike Hnatt wrote:
I'd like the focus to be on "mycontrol" when the page is first loaded.


Other people have told you HOW, but here is WHY NOT.

If you put the focus somewhere when the document loads, you will cause
problems for users who expect the focus to be somewhere else, or who are
already using the focus.

The two main groups of users are those of navigate the page using a focus
based device (e.g. tabbing) rather then a pointing device. They could
already be tabbing through the document when you snatch the focus and put
it somewhere else.

The other group are people using a form on the page (if one exists), they
could be mid way through entering text in to (say) the 3rd field then you
take the focus and shove it in to the 1st. Worse if they don't notice and
tab onwards overwriting all the data they've entered already with data
intended for different fields!

--
David Dorward http://dorward.me.uk/
Jul 20 '05 #5

P: n/a
While the city slept, David Dorward <do*****@yahoo.com> feverishly typed:
Mike Hnatt wrote:
I'd like the focus to be on "mycontrol" when the page is first
loaded.


Other people have told you HOW, but here is WHY NOT.

If you put the focus somewhere when the document loads, you will cause
problems for users who expect the focus to be somewhere else, or who
are already using the focus.


This is very true. I have a solution for this of sorts at:
http://www.html-faq.com/htmlforms/?entryfocus

Cheers,
Nige

--
Nigel Moss.

Email address is not valid. ni***@nigenetDOG.org.uk. Take the dog out!
http://www.nigenet.org.uk | Boycott E$$O!! http://www.stopesso.com
"How strange the change from major to minor..."
Jul 20 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.