This code fixes the issue in Firefox where the onreadystatechange event is not called for synchronous XHR requests. It is based on the XHConn script from http://xkr.us/code/javascript/XHConn/, patched with the fix documented at http://lukav.com/wordpress/2007/04/12/firefox-firebug-and-synchronos-calls-problem/
1
2 /** XHConn - Simple XMLHTTP Interface - bfults@gmail.com - 2005-04-08 **
3 ** Code licensed under Creative Commons Attribution-ShareAlike License **
4 ** http://creativecommons.org/licenses/by-sa/2.0/ **/
5 /**
6 * Modified slightly from original to support synchoronous transactions
7 * CDB 2007-05-16, 2007-06-05
8 */
9
10 function XHConn()
11 {
12 var xmlhttp, bComplete = false;
13 try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
14 catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
15 catch (e) { try { xmlhttp = new XMLHttpRequest(); }
16 catch (e) { xmlhttp = false; }}}
17 if (!xmlhttp) return null;
18 this.connect = function(sURL, sMethod, sVars, fnDone, bAsynch)
19 {
20 if (!xmlhttp) return false;
21 bComplete = false;
22 sMethod = sMethod.toUpperCase();
23
24 if (bAsynch == null) bAsynch = true; //treat asynch as an optional argument
25
26 try {
27 if (sMethod == "GET")
28 {
29 xmlhttp.open(sMethod, sURL+"?"+sVars, (bAsynch == true));
30 sVars = "";
31 }
32 else
33 {
34 xmlhttp.open(sMethod, sURL, (bAsynch == true));
35 xmlhttp.setRequestHeader("Method", "POST "+sURL+" HTTP/1.1");
36 xmlhttp.setRequestHeader("Content-Type",
37 "application/x-www-form-urlencoded");
38 }
39
40 xmlhttp.onreadystatechange = function(){
41 if (xmlhttp.readyState == 4 && !bComplete)
42 {
43 bComplete = true;
44 fnDone(xmlhttp);
45 }
46 };
47 xmlhttp.send(sVars);
48
49 /**
50 * Firefox <= 2.0.0 doesn't fire onreadystatechange for synchronous requests.
51 * See http://lukav.com/wordpress/2007/04/12/firefox-firebug-and-synchronos-calls-problem/
52 */
53 var isGecko = (document.addEventListener) ? true : false;
54 try {
55 if (!bAsynch && isGecko && xmlhttp.onreadystatechange == null) {
56 bComplete = true;
57 fnDone(xmlhttp);
58 return true;
59 }
60 }
61 catch (e) { return false; }
62 /**
63 * End synchronous request patch
64 */
65 }
66 catch(z) { return false; }
67 return true;
68 };
69 return this;
70 }