JavaScript Design Patterns Learning Notes

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.