javascript window.onLoad

Code junkies hangout here

Moderators: ChrisThornett, LXF moderators

javascript window.onLoad

Postby TonyLB » Tue Feb 20, 2007 10:10 pm

I've got an html page which fills several textboxes in a form with data from a database. Some php code initialises the arrays of data (correctly) and then javascript uses this to put the data into the input text boxes.

I've used window.onLoad() to call a javascript function which loads the first record. The first part of the relevant function is:
function fillRecord(){
obj = document.getElementById('name'); obj.value = Name[recordPos];

At first the data is not filled in and the js console reports that 'the element obj has no properties'. If I navigate back to the first record it does get filled in. I've investigated further and it seems as if the function gets called before the form is drawn. Shifting the window.onLoad() call to just before the <\body> tag solves it, but I thought that onLoad waited until the entire page was loaded. Or am I missing something obvious here? I didn't really want scraps of script through the document.

How can I force it to wait until the entire frame, including the form, is loaded and drawn?

Tony
TonyLB
LXF regular
 
Posts: 112
Joined: Tue Apr 12, 2005 7:08 pm
Location: Wirral, UK

RE: javascript window.onLoad

Postby jpmcc » Wed Feb 21, 2007 10:30 pm

Don't know if this will help, but marking a script element with the defer attribute would tell the browser that this code can safely wait until everything else is complete. That may have the effect that you are after.

e.g.
Code: Select all
<script type="text/javascript" defer="defer">

some code...

</script>


Then in that block (which I guess could still be the in the head element), call the function to fill in your text fields.
jpmcc
 
Posts: 2
Joined: Wed Feb 21, 2007 8:54 pm

RE: javascript window.onLoad

Postby TonyLB » Sun Feb 25, 2007 12:51 pm

Thanks, I might give that one a go.

Tony
In the beginning was nothing, which exploded! (Lords and Ladies, Terry Pratchett)
TonyLB
LXF regular
 
Posts: 112
Joined: Tue Apr 12, 2005 7:08 pm
Location: Wirral, UK


Return to Programming

Who is online

Users browsing this forum: No registered users and 1 guest