JavaScript – Auto Set Focus When Page is Done Loading

I use this block of code quite often. Almost every project I develop has a login page and I always toss this in to give it that subtle boost to the overall user experience. Once the page has loaded, the cursor location will automatically be set to the username field so the user doesn’t have to grab their mouse or tab over to it just to log in. I’ve seen far too many site that don’t use this and it always frustrates me.

<script type="text/javascript">
	window.onload = function() {
		document.forms[0].username.focus();
	}
</script>

I would also recommend this on any page that has a form element on it that must be filled out. However be careful not to use it on a page that has a form field that the user may not be interested in. A good example of bad placement for this block of code is AVN Ads which sets focus to the search box instead of the username field. Every time I go there I get half way through typing my username and password before it jumps up to the search box and I in my haste I end up typing my password into the search field for all the world to see.


Tags: , , , , ,

  • Del.icio.us
  • StumbleUpon
  • Reddit
  • Twitter
  • RSS

9 Responses to “JavaScript – Auto Set Focus When Page is Done Loading”

  1. thezilch says:

    If using this technique, consider using document.activeElement first to discover whether you would stealing focus from already focused, form elements or other notable page elements.

  2. baimusenlin says:

    i use jquery
    $(document).ready(function(){

    $(“#loginName”).focus();
    });

  3. You’re my friend, Fettes, but I totally disagree here. The most annoying thing in the world (next to rebooting for Windows Vista updates) is when your cursor moves as you’re typing something because you navigated to a new page. Let me control my own mouse and stop moving the focus.

    A user interface should never move as you go to use it!!!

  4. Eric Wendelin says:

    Agreed with John.

    What if…. we checked if all inputs are empty before focusing anything? This is trivial to do in JavaScript, and would make it a LOT less annoying.

  5. Eric Wendelin says:

    Oh also, document.activeElement was IE-only last I heard. Bad!

  6. Joshua Hatfield says:

    @John I agree to an extent but if you have a commonly used input box on the top of the page, then you should add the focus code directly after the input while disregarding the onload event.

    Example:
    Search:

    s = document.getElementById(‘searchtext’);
    s.value = ”; s.focus();

    Is it shitty to have Javascript outside the head element? Yes. But there are workarounds.

  7. Joshua Hatfield says:

    Edited:

    [Note] — = less than / greater than

    @John I agree to an extent but if you have a commonly used input box on the top of the page, then you should add the focus code directly after the input while disregarding the onload event.

    Example:
    Search: –input type=”text” name=”searchtext” id=”searchtext” value=”” /–
    –script language=”JavaScript” type=”text/javascript”–
    s = document.getElementById(‘searchtext’);
    s.value = ”; s.focus();
    –/script–
    –input type=”submit” name=”submit” value=”Search” /–

    Is it shitty to have Javascript outside the head element? Yes. But there are workarounds.

  8. Srimanta Chakraborty says:

    Thanks a lot for helping me to set the default focus on the text field.

  9. sangita says:

    i have the problem to set focus. In my case when some on change event fire the page become loaded and the cursor move to the address bar of the web page .But i need to keep focus on same element after page load which is focused before.Plzzzzzzzzzzz urgent help me… i have tried no of ways…

    advanced thanks

Leave a Reply