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?
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?
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
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
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