Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10

Thread: focus onload

  1. #1
    Regular Coder
    Join Date
    Dec 2002
    Location
    France
    Posts
    522
    Thanks
    0
    Thanked 0 Times in 0 Posts

    focus onload

    for to give the focus to the first control I have this code :

    <script type="text/javascript" language="javascript">function SetInitialFocus(){ document.frm['t_Login'].focus();}window.onload = SetInitialFocus;</script>


    <form id="frm">
    <input type="text" " id="t_Login" />
    </form>

    is there any way to write all directly in the control itself, xsomething like

    <input type="text" " id="t_Login" onload="SetInitialFocus" />

    ??

    thank you

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    No. Why don't you want that window.onload thing?
    Maybe you want it shorter.

    <script type="text/javascript">
    onload = function(){document.frm['t_Login'].focus()}
    </script>
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #3
    Regular Coder
    Join Date
    Dec 2002
    Location
    France
    Posts
    522
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ok shorter is allready good

    thank you !

  • #4
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    <input type="text" " id="t_Login" onload="SetInitialFocus" />
    Anyway, that seems not correct to me, as onload is an excusive window's event handler.

    window.onload = function (or onload = function , which is the same) and
    <body onload="function"> are correct.

    Probably <tagOtherThanBody onload> will fire exactly like <body onload> but I am affraid that if there will be 2 or more of these, the code will be useless, if not even will bring javascript errors...
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #5
    Regular Coder
    Join Date
    Dec 2002
    Location
    France
    Posts
    522
    Thanks
    0
    Thanked 0 Times in 0 Posts
    then no possibility to write the setFocus directly into the first control without any external script tag ?

  • #6
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    without any external script tag
    but why?

    It is true that you may fire a function using something like

    "javascript:a_function()" but you need by all means an eventhandler to fire it this way....
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #7
    Regular Coder
    Join Date
    Dec 2002
    Location
    France
    Posts
    522
    Thanks
    0
    Thanked 0 Times in 0 Posts
    you can write onkeypress:retrun false;

    then why not
    onload:setFocus;

    ?

    :-)

  • #8
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Because form elements don't load anything. Images, frames, documents - they all load, and that's why they have load events.

    You could try the readyState property - that might return a value, in IE at least.
    Last edited by brothercake; 01-15-2004 at 12:45 PM.
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark

  • #9
    Super Moderator sage45's Avatar
    Join Date
    May 2002
    Posts
    1,063
    Thanks
    0
    Thanked 14 Times in 14 Posts
    angiras - The code provided by glenngv will do exactly what you would like for it to do... You can set focus on an object...

    -sage-
    HTML & CSS Forum Moderator

    "If you don't know what you think you know, then what do you know."
    R.I.P. Derrick Thomas #58
    1/1/1967 - 2/8/2000

  • #10
    Regular Coder
    Join Date
    Dec 2002
    Location
    France
    Posts
    522
    Thanks
    0
    Thanked 0 Times in 0 Posts
    yes The code provided by glenngv works fine ! no prob !

    thanks


  •  

    Posting Permissions

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