Click to See Complete Forum and Search --> : Verifying that a script has been loaded.


tim042849
05-03-2006, 07:00 PM
I'm using a function in the onload handler:
onload="InitForm();"
InitForm needs to call other functions if the javascript files containing
them have already been sourced.

I've used the following method - borrowed from "C" header files:
Each javascript file has a global variable that "announces" the
loading of the files.
Example: for GeoData.js
var GeoData_js = 1;
And in the Initform function I place the following:

var type = typeof(GeoData_js);
if(type.toUpperCase() != 'UNDEFINED'){
InitLists(); // call the function in GeoData.js
}

Initial tests indicate that this should work.
However, I have a minimal knowledge of javascript, so I welcome comments on a better or more compact way to do this.

Thanks
Tim

mrhoo
05-03-2006, 07:44 PM
Simplest is the do or die method:
try{
InitLists();
}
catch(er){
// do nothing; or continue (a loop)
}

tim042849
05-03-2006, 08:58 PM
Thank you mrhoo.

I've just googled try/catch for javascript.
From what I'm seeing, I'm not entirely certain if this is supported across all browsers.
Do you know?

Looks like a better solution ...
thanks
tim

Logician
05-03-2006, 09:57 PM
Thank you mrhoo.

I've just googled try/catch for javascript.
From what I'm seeing, I'm not entirely certain if this is supported across all browsers.
Do you know?
Supported on all modern browsers, but the only way an included file fails to load is if it's not available on the server. Is that likely in your case?

tim042849
05-03-2006, 10:10 PM
Supported on all modern browsers, but the only way an included file fails to load is if it's not available on the server.
Or it is not sourced.
Is that likely in your case?
My idea is that for every page on this site, we have one function call:
Initform()
as an 'onload' handler.
I'm thinking that even though the try/catch construct is valuable to know about,
I should have function calls like InitList() to check for the existance of certain DOM members.

I.E. I should make sure that the page designers are sourcing the script, and I should have the functions in the scripts check for the existence of the the target DOM members.
I guess that I'm changing my approach here...

tim