1. Understand Navigator objects
Navigator: get information about the browser. all browsers support Navigator objects and are read-only.
Ii. attributes of the Navigator object
-- The following reference: http://www.5idev.com/p-javascript_navigator.shtml
Note:
- Navigator. appCodeName: in IE/Firefox/Chrome browsers, the value is "Mozilla ".
- Navigator. appName: Firefox/Chrome are all Netscape.
- Navigator. browserLanguage: Firefox/Chrome returns "undefined ".
- Navigator. cpuClass: Firefox/Chrome returns "undefined ".
- Navigator. onLine: Firefox/Chrome returns "undefined ".
- Navigator. systemLanguage: Firefox/Chrome returns "undefined ".
- Navigator. userLanguagee: Firefox/Chrome returns "undefined ".
1 <script language = "javascript"> 2 // Navigator: get information about the browser. all browsers support the Navigator object. 3 // think: what information does the browser contain? 4 // The Navigator object is a read-only 5 document. write (navigator. appCodeName + "<br/>"); // obtain the code name of the browser: Mozilla 6 document. write (navigator. appName + "<br/>"); // obtain the name of the browser 7 document. write (navigator. appVersion + "<br/>"); // obtain the browser platform and version information 8 document. write (navigator. browserLanguage + "<br/>"); // obtain the language used by the browser (only supported by IE) 9 document. write (navigator. cookieEnabled + "<br/>"); // checks whether the browser enables cookie support and returns a Boolean value of 10 document. write (navigator. cpuClass + "<br/>"); // obtain the CPU level of the system where the browser is located (only supported by IE) 11 document. write (navigator. onLine + "<br/>"); // checks whether the system is in offline mode. A boolean value is returned. false indicates offline (only supported by IE) 12 document. write (navigator. platform + "<br/>"); // obtain the operating system platform of the browser 13 document. write (navigator. systemLanguage + "<br/>"); // obtain the operating system language of the browser. 14 document. write (navigator. userAgent + "<br/>"); // get the value of the user proxy header used by the browser for HTTP requests by 15 16 </script>
Iii. Method of Navigator object
--- See w3cschool.
Iv. Check the version type of IE browser
1 <script language = "javascript"> 2 // check that IE6 prompts an upgrade. a numeric value is returned, indicating that the major version number of IE is 3 function getIEVersion () {4 var ua = navigator. userAgent; // obtain the user information. 5 // check the location of the special string "MSIE". 6 var B = ua. indexOf ("MSIE"); 7 if (B <0) {8 return 0; 9} 10 // extract the version number string and convert it to The value 11 return parseFloat (ua. substring (B + 5, ua. indexOf (";", B); // For example, 8.012} 13 var IEVer = getIEVersion (); 14 if (IEVer = 0) {15 alert ("you are using a non-IE browser. "); 16} else if (IEVer> 6) {17 alert (" your browser version is newer. ") 18} else {19 alert (" You need to upgrade your browser version! "); 20} 21 22 // prompt: The indexOf space also occupies a space, and the number of spaces occupies 23 24/* the output HTTP Proxy header: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0 ;. net clr 2.0.50727 ;. net clr 3.0.20.6.2152 ;. net clr 3.5.30729 ;. NET4.0C ;. NET4.0E). It looks a little complicated. It is not easy for us to extract the version 8.0 in it. We can simplify the format as follows: 25 M4C; MSIE 8.0; windows 26 and then 27 */28 </script>
5. Check the browser type
1 <script language = "javascript"> 2 // check the browser type 3/* although the Navigator object provides the appName attribute to get the browser name, but unfortunately, Firefox, Chrome, and other browsers Based on the Netscape code are all named Netscape. Therefore, the use of navigator. appName does not strictly distinguish different browsers. */4 // Each browser must be strictly differentiated and determined by the specific information contained in the navigator. userAgent attribute. 5 var ua = navigator. userAgent. toLowerCase (); 6 var info = {7 // IE browser 8 ie:/msie/. test (ua )&&! /Opera /. test (ua), 9 // Opera Browser 10 op:/opera /. test (ua), 11 // Safari browser 12 sa:/version. * safari /. test (ua), 13 // Chrome browser 14 ch:/chrome /. test (ua), 15 // Firefox browser 16 ff:/gecko /. test (ua )&&! /Webkit /. test (ua) 17}; 18 19 // judge and output result 20 (info. ie) & alert ("IE browser"); 21 (info. op) & alert ("Opera Browser"); 22 (info. sa) & alert ("Safari"); 23 (info. ff) & alert ("Firefox"); 24 (info. ch) & alert ("Chrome browser"); 25 26 </script>