bodyonload vs <body onload="">

Discussion in 'JavaScript' started by Jamie18, Aug 23, 2007.

  1. #1
    I've been noticing a difference in the two methods.

    bodyonload seems to run before the body has actually loaded, i.e. if i try to access a form element it doesn't exist.

    putting the onload in a body tag seems to create the form than run the onload.

    i need that functionality but i want to do it in the javascript.

    is there another method like, bodyafterload or something that i can use?

    i'm currently using the bodyonload as the last line of the script, if i moved it somewhere else would that change anything?
     
    Jamie18, Aug 23, 2007 IP
  2. ssanders82

    ssanders82 Peon

    Messages:
    77
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Using <body onload=""> has always worked for me...are you creating the form dynamically w/ javascript or something? Or is it just in the HTML?
     
    ssanders82, Aug 24, 2007 IP
  3. Jamie18

    Jamie18 Peon

    Messages:
    201
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    well, <body onload=""> works for me but i'd rather have it separated from the body tag and in the js so if i want i can include form with the page elsewhere without having to add the onload everytime i do. just seems cleaner to me i guess..

    the form is created somewhat dynamically.. not very though.

    it's supposed to load the form with it's various text fields and checkboxes etc. than if javascript is enabled it recreates the options in the select boxes based on what's in the database for that user.

    anyways, once again the problem is.. when i put the bodyonload=function() in the javascript it can't find the select boxes it needs to populate the options for.. it's as though the form hasn't yet loaded
     
    Jamie18, Aug 27, 2007 IP
  4. m0nkeymafia

    m0nkeymafia Well-Known Member

    Messages:
    399
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    125
    #4
    include the javascript file at the end of your html
    this way all the html objects have been created, and itll make the page load faster :)
     
    m0nkeymafia, Aug 28, 2007 IP
  5. Jamie18

    Jamie18 Peon

    Messages:
    201
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #5
    i like the way you think.. seemed to do the trick
     
    Jamie18, Aug 28, 2007 IP
  6. garazy

    garazy Peon

    Messages:
    35
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    You might want to consider using something like Yahoo YUI as using body onload is obtrusive and may prevent any other scripts you include in your page from loading.

    The Yahoo YUI has a method called YAHOO.util.Event.onDOMReady which allows you to run script when the DOM is ready (which isn't necessarily when the page is fulled loaded).

    You can also use a method called YAHOO.util.Event.onAvailable ( id , function )

    Which will execute a function based on an element in your page being available, such as a textbox.

    Gary
     
    garazy, Aug 31, 2007 IP