Why is this Javascript not working with Opera or Chrome?

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.

+4
source share
4 answers

It works for me in Opera ..

Instead of using the Waiter script, you can use the event:

 scrb.onload = function() { FunctionTwo() } scrb.onreadystatechange = function() { FunctionTwo() } 

The second line is for Internet Explorer. The problem is that Opera seems to interfere with both of these events, so FunctionTwo () will execute twice. There are various ways around this. Browser detection, some global variable, etc.

+3
source

You could just add FunctionTwo () to the end of scripttwo.js. It will then work when it boots without the extra complexity of the interval.

+2
source

Some questions / comments that may give you an answer:

  • What causes FuncionOne (pay attention to your spelling)?

  • Timers are dirty and not always recursive (fire again if not stopped). I would refactor Waiter() to check if Interval still exists, and if not, create a recursive window.setInterval .

  • In this note, you may need to explicitly specify window.setInterval without omitting window.

  • Does Interval ? You define it inside the function. Traditional logic will say clearInterval(Interval); in Waiter() will not have access ... But JS is a bit dirty. Opera and Chrome may be a little less dirty than you expect. A simple definition of this parameter outside of any scope should fix this.

+1
source

I think the problem is with the Interval scope. It is defined internally by FunctionOne, but not in a global scope. So, I suspect that when it comes to Waiter execution, Opera and Chrome are faced with the fact that Interval is undefined and just disconnects from FunctionTwo (maybe the script stops?). FF and IE can simply ignore this.

(By the way, what should clearInterval do canonically when it receives the value of the undefined parameter?).

+1
source

Source: https://habr.com/ru/post/1285872/


All Articles