473,388 Members | 1,264 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

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

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
5 9640
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: JCO | last post by:
I have a text box, on my form that I cant get the focus on. I'm using it as shown: <head> <body onload=document.getPassword.txtPasswordName.focus()> </head> Where getPassword is the form &...
7
by: selen | last post by:
I am using above script for Focus to txtAdi which is textbox in c# web application.But it doesnt work. Have you any idea why it doesnt work thanks....... char c=(char)34;
10
by: tshad | last post by:
I have a page where I give focus to the first textbox on my page: <body onLoad="document.forms.firstName.focus();"> I also go and check the status of some data when I exit the 3rd textbox on...
4
by: Aung Thu | last post by:
Hi, there! I want to set focus on a control, of which TabIndex is not the first, when a form is loaded. In the form's load event, I have used Control.Focus() but it doesn't not work. The focus...
2
by: Sridhar | last post by:
Hi, I have a question. I have created a user control which contains a textbox and a button. we will enter some search word in the textbox and hit the button. Then we will get a pop-up with the...
5
by: linuxnooby | last post by:
This is a simple question but it is doing my head in all i want is a form text field to be focused when the page loads. Works in IE6 but not in Firefox 1.6 <body > <form...
4
by: planb | last post by:
Hi, I'd like to have a rollover like effect when a input field has the focus, any idea of how to do this with just CSS (easy enough with javascript)? What I'm thinking of is having a tips box...
5
by: Finn Stampe Mikkelsen | last post by:
Hi How can i set a focus to a textbox in my codebehind page?? I have this WebForm, that takes information from a user and 2 buttons on the form. One that takes action on the entered...
4
by: Jason | last post by:
Hi, Here's the scenario: I have a web application that has window A and window B. A user has both window A and B open - window A is in the foreground and window B is behind it. If the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.