1, some of the more commonly used code can be used as accumulation, so that in the realization of a function can be faster to achieve the purpose of learning the basis is to understand, but to be able to apply in addition to their own writing, should also accumulate better code:
Step through each of the XHR objects until you encounter objects that are supported by the current JavaScript environment.
/ simplexhrfactory Singleton, step 1. /
var simplexhrfactory = (function () {
The three branches.
var standard = {
Createxhrobject:function () { return new XMLHttpRequest ();}
};
var activexnew = {
Createxhrobject:function () { return new ActiveXObject (' Msxml2.xmlhttp ');}
};
var activexold = {
Createxhrobject:function () { return new ActiveXObject (' Microsoft.XMLHTTP ');}
};
})();
/ simplexhrfactory Singleton, step 2. /
var simplexhrfactory = (function () {
The three branches.
var standard = {
Createxhrobject:function () { return new XMLHttpRequest ();}
};
var activexnew = {
Createxhrobject:function () { return new ActiveXObject (' Msxml2.xmlhttp ');}
};
var activexold = {
Createxhrobject:function () { return new ActiveXObject (' Microsoft.XMLHTTP ');}
};
To assign the branch, try each method; Return whatever doesn ' t fail.
var testobject;
try {
Testobject = Standard.createxhrobject (); return standard; Return this if no error is thrown.
}
catch (e) {
try { testobject = Activexnew.createxhrobject (); return activexnew; Return this if no error is thrown.} catch (E) { try { testobject = Activexold.createxhrobject (); return activexold; Return this if no error is thrown. } catch (e) { throw new Error (' No XHR object found in this environment. ');} }
}
})();
2. Common methods for obtaining current browser information:
//=========
var browser=navigator.appname
var b_version=navigator.appversion
var version=parsefloat (b_version)
document.write ("Browser name:" + browser)
document.write ("
“)
document.write ("browser version:" + version)
//============
document.write ("
Code: ")
document.write (Navigator.appcodename + "
“)
document.write ("
Platform: ")
document.write (Navigator.platform + "
“)
document.write ("
Cookies enabled: ")
document.write (navigator.cookieenabled + "
“)
document.write ("
User Agent header for browser: ")
document.write (Navigator.useragent + "
“)
//=========
var x = navigator;
document.write ("codename=" + x.appcodename);
document.write ("
“);
document.write ("minorversion=" + x.appminorversion);
document.write ("
“);
document.write ("name=" + x.appname);
document.write ("
“);
document.write ("version=" + x.appversion);
document.write ("
“);
document.write ("cookieenabled=" + x.cookieenabled);
document.write ("
“);
document.write ("cpuclass=" + x.cpuclass);
document.write ("
“);
document.write ("online=" + x.online);
document.write ("
“);
document.write ("platform=" + x.platform);
document.write ("
“);
document.write ("ua=" + x.useragent);
document.write ("
“);
document.write ("browserlanguage=" + x.browserlanguage);
document.write ("
“);
document.write ("systemlanguage=" + x.systemlanguage);
document.write ("
“);
document.write ("userlanguage=" + x.userlanguage);
JavaScript Design Patterns Learning Notes