[thelist] Javascript: determining if a element has focus?

Peter-Paul Koch gassinaumasis at hotmail.com
Fri May 14 07:52:44 CDT 2004



>Not sure if this is possible, but figured I would ask.
>
>I've got my spiffy login form, and when the page loads, I want to point the 
>focus at
>the username field.
>
>However - I ***HATE*** it when a page loads slowly, and I'm halfway through 
>typing in
>my password, and it shifts my focus back to the username... and suddenly 
>I've typed
>half my password in the username field.
>
>So I want to check if the focus is in either the username or password 
>fields, and if
>it is, I won't set the focus to the username field.
>
>All that being said - anyway I can check if a field has focus, short of 
>using the
>onFocus and onBlur events to set some variables on the page?

Only way I can think of is setting the focus not onLoad but directly after 
the form field has been parsed:

<input name="username" id="username">
<script>
document.getElementById('username').focus()
</script>
<input name="password">

(I deliberatey don't use the document.forms[] syntax because a browser might 
create these objects only after the page has been loaded completely. 
getElementById() is much safer in this respect).

-------------------------------------------------------------------
ppk, freelance web developer
Interaction, copywriting, JavaScript, integration
http://www.quirksmode.org/
Column "Keep it Simple": http://www.digital-web.com/columns/keepitsimple/
------------------------------------------------------------------

_________________________________________________________________
MSN Search, for accurate results! http://search.msn.nl



More information about the thelist mailing list