To obtain the browser information string in js, you only need to use navigator. userAgent. In this way, you can use indexof to determine the version or other information. The following describes several instances.
The use of Javascript userAgent information retrieval is suitable for browsers that have been released before. Today, more concise small code is collected and can be encapsulated into a function for use! Very good results.
Js's alert (navigator. userAgent. toLowerCase (); method to get the browser's userAgent information:
Only one string of characters shown above, if we want to be more user-friendly, we can add one to judge
The Code is as follows: |
Copy code |
Var userAgentInfo = navigator. userAgent. toLowerCase (); Var Agents = new Array ("android", "iphone", "symbianos", "windows phone", "ipod "); For (var I = 0; I <Agents. length; I ++ ){ If (userAgentInfo. indexOf (Agents [I])> = 0 ){ Alert ("www. bKjia. c0m "); Break; } } |
Based on the above example, we can make a browser compatibility judgment
The Code is as follows: |
Copy code |
<Script type = "text/javascript"> // Identify whether to use IE browser If (navigator. userAgent. indexOf ("MSIE")> 0) { // If the result is IE8.0, ie8.CSS is used. If (navigator. userAgent. indexOf ("MSIE 8.0")> 0) { Css. innerHTML = '<link href = "/ie8.css" rel = "stylesheet" type = "text/css">' } // If the result is IE6.0, ie6.CSS is used. If (navigator. userAgent. indexOf ("MSIE 6.0")> 0) { Css. innerHTML = '<link href = "/ie6.css" rel = "stylesheet" type = "text/css">' } // Otherwise, use css. CSS } Else { Css. innerHTML = '<link href = "www. bKjia. c0m/css.css" rel = "stylesheet" type = "text/css">' } </Script> |
The above is a simple test, as shown below:
The Code is as follows: |
Copy code |
<Script type = "text/javascript"> Var Sys = {}; Var ua = navigator. userAgent. toLowerCase (); If (window. ActiveXObject) Sys. ie = ua. match (/msie ([d.] +)/) [1] Else if (document. getBoxObjectFor) Sys. firefox = ua. match (/firefox/([d.] +)/) [1] Else if (window. MessageEvent &&! Document. getBoxObjectFor) Sys. chrome = ua. match (/chrome/([d.] +)/) [1] Else if (window. opera) Sys. opera = ua. match (/opera. ([d.] +)/) [1] Else if (window. openDatabase) Sys. safari = ua. match (/version/([d.] +)/) [1];
// Perform the following tests: If (Sys. ie) document. write ('ie: '+ Sys. IE ); If (Sys. firefox) document. write ('Firefox: '+ Sys. Firefox ); If (Sys. chrome) document. write ('chrome: '+ Sys. Chrome ); If (Sys. opera) document. write ('Opera: '+ Sys. Opera ); If (Sys. safari) document. write ('safari: '+ Sys. Safari ); </Script> |
This is comprehensive.