Only registered users can comment.

  1. 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. 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!!!

  3. 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.

  4. @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.

  5. 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.

  6. 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

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.