Firefox does not start onreadystatechange

I made javascript code for my site, it works without problems on opera and chrome, but not on firefox.

Here is the script:

function checkstate(who,row,cell) { var zwrot=""; var mouseEvent='onmouseover="javascript:bubelon(this.id);" onmouseout="bubeloff();"'; var cellid=""; ajax=new XMLHttpRequest(); ajax.onreadystatechange=function(aEvt) { if (ajax.readyState===4 && ajax.status===200) { alert("im here!"); } }; ajax.open('GET',"oth/work_prac_stan.php?usr="+who,false); ajax.send(); } function sprawdzstan() { var lol=""; var table = document.getElementById("usery"); var re = /^<a\shref\=/g; for (var i = 1, row; row = table.rows[i]; i ++) { if (row.cells[0].innerHTML.match(re)) { checkstate(row.cells[1].innerHTML,row,2); } else { checkstate(row.cells[0].innerHTML,row,1); } } } 

The problem is that firefox does not run the function assigned by onreadystatechange. I checked in firebug that the response from the php file is correct.

Where is the problem? It works on chrome and opera, there is simply no firefox, there is no error in the console.

+4
source share
2 answers

Updated Answer

According to Mozilla docs , you are not using onreadystatechange with synchronous requests. Which makes sense since the request is not returned until the ready state is 4 (completed), although I probably would not have designed it that way.

Original answer

It is not immediately possible to see a smoking gun, but: Your ajax variable is not defined inside the function, and therefore you almost certainly rewrite it at each iteration of the loop in sprawdzstan . Does this remain a problem since you are using a synchronous ajax call. In any case, add var ajax; at checkstate to make sure you don’t fall prey to the horror of implicit globals .


Off topic . If you can find a way to reorganize your project so as not to use a synchronous ajax request, strongly recommend doing this. Synchronous requests block the browser user interface (more or less depending on the browser, but many of them are completely blocked, including other unrelated tabs). You can almost always reorganize and use an asynchronous request.

Beyond topic 2 : you are not using mouseEvent in your code, but if you were, you would like to get rid of these javascript: prefixes javascript: onmouseover and onmouseout . These attributes are not URLs, the prefix is ​​not (there) a protocol qualifier (this is a label that you are not using).

+4
source

For those who are still facing this problem ...

You can use the code below. What I did was remove the function

 ajax.onreadystatechange=function(aEvt) { 

and pass a warning ("im here!"); after ajax.send ();

 ajax=new XMLHttpRequest(); ajax.open('GET',"oth/work_prac_stan.php?usr="+who,false); ajax.send(); alert("im here!"); 
0
source

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


All Articles