Identify browser code using JS userAgent

Source: Internet
Author: User

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.

Related Article

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.