Thank you for reading.
I have several scripts that are built something like this:
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 works fine in script one. However, nothing that should happen in scripttwo.js actually happens. As if scripttwo.js is not turning on.
Any ideas why this does not work with Opera or Chrome?
Perhaps I am using something that is incompatible, or are there security features that I donโt know about? All files are in the same domain.
Note Great answers - thank you very much!
FuncionOne is just a typo here, in the code itself, I use the best function names, but I changed them here for readability. This may be the scope, although I agree with Joe White that this should not be a problem. With JavaScript (one of my weakest languages), who knows? FunctionOne is called from the head or body of an HTML document.
I also like the idea of โโadding FuncTwo to the end of script two to completely avoid the timer. Cleaner and so obvious as soon as someone points out this ...
I will update after I work on it.
Refresh again:
Hello to all,
Now I work in FF, IE and Chrome, but Opera doesn't seem to want to download .js files at all. I think this is just an Opera problem ( Opera: the .js file does not load ), and will continue with the other three. Tell me how it turns out.