View Full Version : Calling script functions after document is loaded
greasonwolfe
03-09-2006, 03:13 AM
I know it has been discussed here more than once, and, at the moment, I cant seem to find any of the threads. In any event, I have a script that I need to execute only after the entire document has loaded. I know I can place this script at the end of the document (right before the body closing tag) and rather than assign it a function, just have it auto execute, however, if I wanted to place it up in the head section of the document, how would I go about calling the script only after the entire document has loaded?
GW
window.onload=someFunc;
or
window.onload=function(){
someFunc();
otherFunc()
}
greasonwolfe
03-09-2006, 03:19 AM
And that would go just before the body closing tag, right?
no, anywhere in the document, general practice is in the head
greasonwolfe
03-09-2006, 03:24 AM
Hrm. . . I am gonna have to think on this one then. I've tried that before and gotten errors, but I think I have a work around. Worst case scenario is that I just autoexecute the script at the end of the document instead. Not as pretty, but it will work.
Thanks for the info. :thumbsup:
GW
The only way you can get errors (in modern browsers) is if an assignment to window.onload already exists in another script. In that case you can just cut and paste the functions into one assignment.
greasonwolfe
03-09-2006, 03:34 AM
Great. I will keep that in mind. I think this is going to solve a problem I have been trying to work through for a couple of weeks now. Basicly I am writing from a parent window to a child window and have been trying to set it up so that if the user attempts to save the child window, data isn't lost. If my logic is correct (and, for that matter, my scripting) I think this just might do the trick as long as the fuction calls occur at the right time.
Thanks again!
GW
glenngv
03-09-2006, 03:48 AM
Common mistake is assigning the onload handler with ().
window.onload=someFunc();
There should be no () as you're assigning a function pointer to the onload handler not immediately calling the function. If the function has parameters, you would use an anonymous function:
window.onload=function(){
someFunc(param1, param2);
}
Another possible mistake is onload conflict.
There's window.onload=someFunc and <body onload="anotherFunc()">.
The last one will take effect as it overrides the previously declared handler.
greasonwolfe
03-09-2006, 05:09 AM
Gotcha. All good info to know. I think I got a good handle on what I want to do now. But I am going to have to go through the coding with a fine toothed comb. Trying to write a script from a parent page into a popup, so that it can handle data placement rather than handling it from the popup. The big problem is going to be getting the variables passed over. The parent page has several arrays that I am going to have to compress. In effect it is kind of like this
On the parent page;
var first=new Array()
var second=new Array()
var third=new Array()
on the popup, they are going to have to look like this
var data=new Array()
data[0]=new Array(first[0],second[0],third[0])
data[1]=new Array(first[1],second[1],third[1])
and so on.
As I said, I think I got a handle on it, just going to have to make sure I escape out the single quotes when I am supposed to. The key is in getting the values of first, second and third actually written out to the popup so that they aren't lost if the visitor saves the page. I don't think it is going to be very pretty, but once I have it done and working properly, then I can worry about making it pretty.
Thanks for the info, guys. It really does help.
GW
glenngv
03-09-2006, 05:39 AM
YOu don't have to create another array in the popup. Just access directly the array in the parent from the popup like this:
opener.first[index]
opener.second[index]
opener.third[index]
All global variables and functions are properties and methods of the window object. So if you have a global variable named "myVar", you can access it in 3 different ways:
myVar
window.myVar
window["myVar"]
Same with functions:
myFunc()
window.myFunc()
window["myFunc"]()
opener is a window object, so you can access the opener variables and methods like this:
opener.myVar
opener["myVar"]
opener.myFunc()
opener["myFunc"]()
greasonwolfe
03-09-2006, 06:28 AM
Right, I understand that part and have fiddled with it. Unfortunately, I haven't gotten any of those methods to work the way I am hoping for. My goal (and it may turn out to be impossible) is to allow the user to save the popup to their harddrive for later viewing. To this end, the variables from the parent window have to be "hard written" into the second window. Hopefully that makes sense, but I will try to illustrate it better.
In the parent window, I am generating a series of arrays based on the values a user supplies to a form. There are a total of 15 arrays and the data elements of each array are used specifically for display purposes in the popup window. My problem has been in physically writing those values in the popup window such that they become their own arrays. To that end, I cant access them through the opener method from the popup window because that simply (or so I understand) grabs the values and uses them. If the popup is saved to the harddrive and then later opened offline, it will look to the parent page for those values and since the parent page isn't open and holding those values, the popup(now saved) page wont find them. Thus, I need to actually write the values into the coding of the popup. It's a mess, I know, but I am trying to find a way to save the user from having to start from scratch each time they want to see the data displayed. I understand they could, perhaps, print it, but if they have several dozen or even hundreds of data sets that they have entered, that could result in several pages worth of printing, so my thought was to allow the user to save it as an offline web page that they could view whenever they wanted.
Anyways, for the most part, I have to figure out a way to write those values into the popup in such a way that if the user enters something like "joe" in the form on the parent page, it is physically transfered to the popup page in exactly that manner.
For example, if the parent generates the following values based on user input;
Name[0]=user enters Joe
Name[1]=user enters Betty
Name[2]=user enters Frank
Age[0]=user enters 25
Age[1]=user enters 15
Age[2]=user enters 35
Job[0]=user enters Plumber
Job[1]=user enters Waitress
Job[2]=user enters Car Salesman
and it is sent to the popup window, it actually has to appear in the source code for the popup window in the following fashion;
Name[0]=Joe
Name[1]=Betty
Name[2]=Frank
Age[0]=25
Age[1]=15
Age[2]=35
Job[0]=Plumber
Job[1]=Waitress
Job[2]=Car Salesman
I know it is messy right now and eventually I will get it cleaned up, hopefully without going completely bald in the process. When I have experimented with the methods you suggest, I don't see the physical values written out in the sourcecode of the popup. Of course, I could be implementing thin wrong and I am still experimenting with them. I am beginning to suspect, however, that I am attempting something just a bit beyond my skill level at this point. LOL. I should stick with working on engines.
GW
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.