createXHR(): 创建兼容浏览器的XMLHttpRequest
1 function createXHR() 2 { 3 if (typeof XMLHttpRequest != "undefined") 4 { 5 return new XMLHttpRequest(); 6 } // end if 7 else if (window.ActiveXObject) 8 { 9 var aVersions = ["MSXML2.XMLHttp.6.0", "MSXML2.XMLHttp.3.0"]; 10 for (var i = 0; i < aVersions.length; ++i) 11 { 12 try 13 { 14 var oXHR = new ActiveXObject(aVersions[i]); 15 return oXHR; 16 } // end try 17 catch (oError) 18 { 19 // do nothing 20 } // end catch 21 22 } // end for 23 24 } // end else if 25 26 throw new Error("XMLHttp object could not be created.") 27 28 } // end createXHR();
来源: professional ajax 2nd edition, page39
1 function createXHR() 2 { 3 try { return new XMLHttpRequest(); } catch (e) {} 4 try { return new ActiveXObject("Msxml2.XMLHTTP.6.0"); } catch (e) {} 5 try { return new ActiveXObject("Msxml2.XMLHTTP.3.0"); } catch (e) {} 6 try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {} 7 try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {} 8 return null; 9 } // end createXHR()
来源: ajax完全手册中文版 62页