Javascript uses navigator. userAgent to identify various browsers

Source: Internet
Author: User

Principle: Recognition Based on the return value of navigator. userAgent

Implementation:
Copy codeThe Code is as follows:
Function validBrowser (){
Var u_agent = navigator. userAgent;
Var browser_name = 'failed' to identify the browser ';
If (u_agent.indexOf ('Firefox ')>-1 ){
Browser_name = 'Firefox ';
} Else if (u_agent.indexOf ('chromi')>-1 ){
Browser_name = 'chrome ';
} Else if (u_agent.indexOf ('trigger')>-1 & u_agent.indexOf ('rv: 11')>-1 ){
Browser_name = 'ie11 ';
} Else if (u_agent.indexOf ('msie ')>-1 & u_agent.indexOf ('trigger')>-1 ){
Browser_name = 'ie (8-10 )';
} Else if (u_agent.indexOf ('msie ')>-1 ){
Browser_name = 'ie (6-7 )';
} Else if (u_agent.indexOf ('Opera ')>-1 ){
Browser_name = 'Opera ';
} Else {
Browser_name + = ', info:' + u_agent;
}
Document. write ('browser _ name: '+ browser_name +' <br> ');
Document. write ('U _ agent: '+ u_agent +' <br> ');
}

3. Attach several browsing user_agent .. For example, The compatible mode of 360 uses IE, and the fast mode uses the chrom kernel.
 

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.