tags:

views:

147

answers:

4

And if there isn't, is there a good reason why?

+1  A: 

Is TABINDEX=0 not working for you?

EDIT:

Sorry - hurried my answer. TABINDEX=0 will work only if the user hits the "TAB" key. Sorry about that. The following will set focus properly on load. Tested in latest IE, FF, Opera, Safari, & Chrome.

<form>
     <input id="first" tabindex="1" /><br/>
     <input id="second" tabindex="2" />
   <script>
 document.getElementById("first").focus();
   </script>
</form>
Chuck
Doesn't work for me.
Noon Silk
A: 

No.

And probably no good reason; everything seems obvious in hindsight.

Noon Silk
+1  A: 

There isn't. There's not really a good reason, other than everyone grew used to using focus() instead. (Unfortunately focus() has drawbacks if the whole page doesn't load and focus straight away.)

I would love to see a both default-focus and default-submit-button designation available in HTML, but the browser folk don't seem to be interested. Edit: as John said, this is now in the HTML5 draft, though there's no implementation yet and HTML5 is far from finalised. We'll see!

bobince
I love the sites which make a focus() call to the user login field. Normally when I start entering my password, the page finally loads, and moves my focus from the password box to the username box. At this point, anyone sitting next to me sees my password in the username box.
brianegge
Indeed. There are workarounds with sniffing where the focus is and not focusing if you've already got focus, but they're annoying and browser-specific. If you must `focus()`, it's probably best to do it in inline script immediately after the field in question. Certainly not onload.
bobince
+1  A: 

This is coming as a part of HTML 5, so the lack of it in prior version is probably more of an oversight or a case of there being other options lowering the priority.

If you're curious, the syntax will be something like:

<input type="text" name="abc" value="" autofocus>

By the standard, it must only be declared once on a page.

In the meanwhile, with the state of the nation, you can only really do it with script in the onload event. The easiest way, is to assign the default element on the page a consistent id (call it 'autofocus') and then always set it like:

var a_focus = document.getElementById('autofocus');
if(a_focus) a_focus.focus();

Hope that helps.

John Cavan