Hi All,
Thanks for reading.
I have several scripts that are built similar to the following:
scriptone.js
function FunctionOne(){
// Do a bit of work...
// Include a second javascript file, scripttwo.js
// that contains a function called FunctionTwo.js
var scrb = document.createElement('script');
scrb.type = 'text/javascript';
scrb.src = 'http://www.example.com/scripttwo.js?bunchofargs=varied';
// Append it to the head.
document.getElementsByTagName('head')[0].appendChild(scrb);
// Can't run the second function directly, because it may not be loaded quite yet,
// So use the Waiter function.
Interval = setInterval("Waiter()", 10);
// All done.
return;
}
function Waiter(){
if(window.FunctionTwo) {
clearInterval(Interval);
FunctionTwo();
}
}
scripttwo.js
function FunctionTwo(){
document.write('something based on calling page');
}
This works fine with FF and IE, but not with Opera or Chrome. In Chrome/Opera, everything seems to work ok in script one. However, nothing that should be happening in scripttwo.js actually happens. It's as if scripttwo.js didn't get included.
Any ideas why this doesn't work with Opera or Chrome?
Perhaps I'm using something that isn't compatible, or are there security features I am not aware of? All files are on the same domain.
Note Great replies - thanks so much!
FuncionOne is just a typo here, in the actual code, I use better function names, but I changed them here for readability. It may be the scope, though i agree with Joe White that it shouldn't be an issue. With JavaScript (one of my weak languages), who knows? FunctionOne is called from either the head or body of the HTML document.
I also like the idea of adding FuncTwo to the end of script two, to avoid the timer altogether. Cleaner, and so obvious once somebody points it out to you...
I will update after I work on this next.
Update Again:
Hi All,
I now have it working in FF, IE, and Chrome, but Opera seems to refuse to load any .js files at all now. I'm thinking this is just an Opera issue of some sort (http://stackoverflow.com/questions/1053676/opera-js-file-wont-load), and will proceed with the other three. Let you know how it turns out.