SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Zealot iStudent's Avatar
    Join Date
    Mar 2004
    Location
    So Cal
    Posts
    126
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How To Load Function Without The Body Tag

    I am trying to load this function to put the cursor in the field

    Code:
    function sf() {document.f.q.focus();}
    I've tried doing this:

    Code:
    window.onload = sf();
    But it didn't work

  2. #2
    Maniacally depressed robot poncho's Avatar
    Join Date
    Dec 2004
    Location
    Belfast, N.Ireland
    Posts
    452
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try:

    PHP Code:
    window.onload sf
    Perfecting the art of breaking stuff.
    Check 'em: CakePHP | TextMate

  3. #3
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    window.onload = sf();
    The parentheses '( )' is the function execution operator. So, the right side of that assignment statement says to execute the function sf. Since calls to functions are replaced by the function's return value, that statement gets converted to:

    window.onload = someValue;

    What you want to do is assign a function reference to onload. That way nothing will execute immediately, and when the onload event fires, js will call the function and execute it for you. A function reference is just the name of the function: sf.

  4. #4
    ♪♪ ♪ ♪ ♪ ♪♪ ♪ ♪♪ Markdidj's Avatar
    Join Date
    Sep 2002
    Location
    Bournemouth, South UK
    Posts
    1,551
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Code:
    window.onload=function(){
    sf();
    }
    hope it helps
    LiveScript: Putting the "Live" Back into JavaScript
    if live output_as_javascript else output_as_html end if

  5. #5
    SitePoint Zealot iStudent's Avatar
    Join Date
    Mar 2004
    Location
    So Cal
    Posts
    126
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That worked!!! A million thanks!!!!

    Quote Originally Posted by Markdidj
    Code:
    window.onload=function(){
    sf();
    }
    hope it helps

  6. #6
    SitePoint Enthusiast
    Join Date
    Jan 2005
    Location
    Canada
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok here is the best way to do it that I have found... make a function called init() like meaning initiate, now within that funtion you can place all the functions that you want to load when your page loads. And then somewhere in the body of your page place this:
    Code:
    if( window.addEventListener ) {
    	window.addEventListener('load',init,false);
    } else if( document.addEventListener ) {
    	document.addEventListener('load',init,false);
    } else if( window.attachEvent ) {
    	window.attachEvent('onload',init);
    }
    Obviously if you didn't want to make the init function and have a fussy() instead the code would look like this:
    Code:
    if( window.addEventListener ) {
    	window.addEventListener('load',fussy,false);
    } else if( document.addEventListener ) {
    	document.addEventListener('load',fussy,false);
    } else if( window.attachEvent ) {
    	window.attachEvent('onload',fussy);
    }
    I have tested this code in ie and firefox, I dunno about the others, but there is three conditions there so it should work on most. you could always add another
    Code:
    else{
             window.onload=init;
    }
    if you wanted to be safe.

  7. #7
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,837
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    you probably don't need the document.addListener since onload belongs to the window and not to the document.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  8. #8
    SitePoint Enthusiast
    Join Date
    Jan 2005
    Location
    Canada
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    nope the document.addEventListener covers opera 7 and perhaps netscape
    and the window.attachEvent gets IE... at least the new one, and window.addEventListener is for gecko, safari, konqueror and standard


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
  •