www.webdeveloper.com
Results 1 to 5 of 5

Thread: focus question

  1. #1
    Join Date
    Jan 2011
    Posts
    39

    focus question

    I am using onload="f();g();document.forms[0].elements[0].focus();" in the body tag. F() and g() are other functions that seem to work ok.

    When I get to the page from the address bar, the focus goes to the address bar. When I right click and refresh the page, focus goes to the first element like I want.

    What could I be doing wrong?

  2. #2
    Join Date
    Dec 2002
    Location
    St. Louis, MO, USA
    Posts
    1,582
    Unless the form presence is dynamically determined, I'd use the name of the form when referencing it; and even then you can use a conditional to check for whether or not the form is present when the page loads.

    Instead of using the onload in BODY, try this:

    Code:
    <script type="text/javascript">
    function init(){
        if(document.formName) {
    	document.formName.inputName.focus();
                      }
    	}
    window.onload = init;
    </script>
    ^_^

  3. #3
    Join Date
    Jan 2011
    Posts
    39
    Took out the document.forms[0].elements[0].focus() from
    onload="a(arglist 1);a(arglist 2);document.forms[0].elements[0].focus()"
    and put in init() etc. No luck and a() did not run.
    Maybe body of form not there when page loads though like you suggested.
    What could cause that ?

  4. #4
    Join Date
    Jan 2011
    Posts
    39
    Made it onload="a(args);a(args);startout()" and in startout, put
    if (document.formname)
    alert('*');
    document.formname.elementname.focus();

    The * shows whether I start from address bar or right click refresh and whether I use formname or forms[0] etc. But still focusing only ok when refreshing.

    What is different about starting from address bar and right click refreshing ?

  5. #5
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Quote Originally Posted by neginf View Post
    Took out the document.forms[0].elements[0].focus() from
    onload="a(arglist 1);a(arglist 2);document.forms[0].elements[0].focus()"
    and put in init() etc. No luck and a() did not run.
    Maybe body of form not there when page loads though like you suggested.
    What could cause that ?
    maybe you have another code which uses also window.onload. Or you can not have more than a single event of one kind on the same element. Check that, and if so, gather all the functions/statements you want to start onload within the same and unique window.onload.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles