SitePoint Sponsor

User Tag List

Results 1 to 16 of 16
  1. #1
    SitePoint Guru Richi's Avatar
    Join Date
    Jul 2001
    Location
    Hu,Bp.
    Posts
    842
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    document.userform.name.focus(); .. whay .. ?

    hi ..

    i wanna focus on input name, but didn`t work , whay ?
    PHP Code:
            <script language=\"JavaScript\">
            <!--
            document.userform.name.focus();
            // -->
            </script> 

  2. #2
    + platinum's Avatar
    Join Date
    Jun 2001
    Location
    Adelaide, Australia
    Posts
    6,441
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try somthing like this:

    PHP Code:
    <script language="JavaScript">
            <!--
            
    document.forms[0].elements[1].focus();
            
    // -->
            
    </script> 
    You can change the values in the [x] so they represent where you're field is
    Last edited by platinum; Jun 4, 2002 at 15:47.

  3. #3
    + platinum's Avatar
    Join Date
    Jun 2001
    Location
    Adelaide, Australia
    Posts
    6,441
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it might of been because of that extra slash you had in there

  4. #4
    SitePoint Guru Richi's Avatar
    Join Date
    Jul 2001
    Location
    Hu,Bp.
    Posts
    842
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    didn`t work ...

    it might of been because of that extra slash you had in there
    this js come in php code !

  5. #5
    + platinum's Avatar
    Join Date
    Jun 2001
    Location
    Adelaide, Australia
    Posts
    6,441
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry, I don't really know PHP

    are you using an onload() in there somwhere?

  6. #6
    SitePoint Guru Richi's Avatar
    Join Date
    Jul 2001
    Location
    Hu,Bp.
    Posts
    842
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    no , it on the head tag, must work, but i don`t know whay !

  7. #7
    SitePoint Columnist Skunk's Avatar
    Join Date
    Jan 2001
    Location
    Lawrence, Kansas
    Posts
    2,066
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's the cause of your problem then. Javascript in the <head> of a document won't run when the page loads unless you call it from the onLoad event of the body tag. Try this:
    Code:
    <body onLoad="document.userform.name.focus();">
    You could also wrap the javascript in a function and call the function from the onLoad attribute.

  8. #8
    SitePoint Guru Richi's Avatar
    Join Date
    Jul 2001
    Location
    Hu,Bp.
    Posts
    842
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    then check the hotmail.com the login page, its not on body tag !

  9. #9
    SitePoint Columnist Skunk's Avatar
    Join Date
    Jan 2001
    Location
    Lawrence, Kansas
    Posts
    2,066
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You're right - however it's not in the <head> part either. It looks like it will work if you put the code somewhere in the body tag.

  10. #10
    SitePoint Guru Richi's Avatar
    Join Date
    Jul 2001
    Location
    Hu,Bp.
    Posts
    842
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ehhh ... this maybe hotmail secter .. didn`t work also !

  11. #11
    SitePoint Wizard Bill Posters's Avatar
    Join Date
    Dec 2001
    Location
    UK
    Posts
    1,523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Skunk
    That's the cause of your problem then. Javascript in the <head> of a document won't run when the page loads unless you call it from the onLoad event of the body tag
    As a general statement, that is plainly wrong.

    There may be instances where it is better to wrap script in a function or use it directly in the body tag, but there are many 1000s of perfectly good cases where javascript is used in the header without being wrapped in a function or being called in the body tag.
    Many (if not most) redirection/sniffer scripts are used in just that manner.
    New Plastic Arts: Visual Communication | DesignateOnline

    Mate went to NY and all he got me was this lousy signature

  12. #12
    Currently Occupied; Till Sunda Andrew-J2000's Avatar
    Join Date
    Aug 2001
    Location
    London
    Posts
    2,475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok, well, the reason it will not work if you place the focus before the object(the textbox) is because when it is loaded it will search for the text box before its loaded, to fix this you can have it any where in the document as long as its in a function which can be referenced in the textbox by

    onload="this.focus()" or calling the function.

    if you look at hotmail they have referenced there call at the end of there document with this

    PHP Code:
    <script language="JavaScript">
    <!--
    document.passwordform.login.focus();
    document.passwordform.passwd.value='';
    document.passwordform.js.value="yes";
    function 
    CheckSecurity()
    {
    if (
    document.passwordform)
    {
    if (
    document.passwordform.sec[0].checked)
    document.passwordform.mspp_shared.value "1";
    }
    return 
    true;
    }
    // -->
    </script> 
    hope thats cleared this up a bit.

  13. #13
    Currently Occupied; Till Sunda Andrew-J2000's Avatar
    Join Date
    Aug 2001
    Location
    London
    Posts
    2,475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Bill Posters


    As a general statement, that is plainly wrong.

    There may be instances where it is better to wrap script in a function or use it directly in the body tag, but there are many 1000s of perfectly good cases where javascript is used in the header without being wrapped in a function or being called in the body tag.
    Many (if not most) redirection/sniffer scripts are used in just that manner.
    hehe you got in before me.

    but, you should always try to have the bulk of your javascript in the head of the document or in an external javascript which can be called by a function call. It is then easier to see or read then.

    however some javascript maybe programmed inline, with the document which is smaller in length and make it easier to reference objects, however you can still do this with function calls such as

    onclick="myFunc(this)"

    so i tend or prefer to put all javascript in the head of the document and depending on the length of all the javascript (one page plus normally) i will end up putting it in a external javascript file

  14. #14
    SitePoint Guru Richi's Avatar
    Join Date
    Jul 2001
    Location
    Hu,Bp.
    Posts
    842
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    lol ... toooo simple

    thx ..

  15. #15
    SitePoint Columnist Skunk's Avatar
    Join Date
    Jan 2001
    Location
    Lawrence, Kansas
    Posts
    2,066
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK this is odd. I just tried the following and it worked fine:
    Code:
    <html>
    <head><title>test</title>
    <script>
    function doit() {
      document.f.q.focus();
    }
    </script>
    </head>
    <body onLoad="doit();">
    <form name=f>
    <input type=text name=q value="">
    </form>
    </body>
    </html>
    Meanwhile the following code did NOT work:
    Code:
    <html>
    <head><title>test</title>
    <script>
    document.f.q.focus();
    </script>
    </head>
    <body>
    <form name=f>
    <input type=text name=q value="">
    </form>
    </body>
    </html>
    This would support my statement that javascript in the <head> part of the code is not run when the page loads.

    Then I tried the following:
    Code:
    <html>
    <head><title>test</title>
    <script>
    alert("hi");
    </script>
    </head>
    <body>
    <form name=f>
    <input type=text name=q value="">
    </form>
    </body>
    </html>
    And recived the alert box when the page loaded, which suggests that code in the <head> IS executed at load time.

    I know you can declare variables and things in the <head> and they work fine, so it seems that some operations (such as document.f.q.focus() in the above examples) don't work when the page is loaded and others do.

    Incidentally, a quick way of trying the above code samples is to use this page and paste them in the text box.

    Anyone got any ideas as to why some JS in the head works runs the page is loaded and other code doesn't?

  16. #16
    SitePoint Columnist Skunk's Avatar
    Join Date
    Jan 2001
    Location
    Lawrence, Kansas
    Posts
    2,066
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cancel that, I just spotted the answer to my question in Andrew-J2000's previous post The document.f.q.focus() thing wasn't working because the rest of the document (with the actual form in it) had not yet been parsed, so the referenced objects didn't exist. Makes sense when you think about it

    I happily retract my earlier statement.


Bookmarks

Posting Permissions

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