JavaScript detects the browser type and version of code _javascript tips

Source: Internet
Author: User
Tags numeric numeric value
Code to detect the browser and its version
Copy Code code as follows:

Getbrowser:function () {
var browser = {
Msie:false, Firefox:false, Opera:false, Safari:false,
Chrome:false, Netscape:false, appname: ' Unknown ', version:0
},
useragent = Window.navigator.userAgent.toLowerCase ();
if (/(Msie|firefox|opera|chrome|netscape) \d+ (\d[\d.) *)/.test (useragent)) {
BROWSER[REGEXP.$1] = true;
Browser.appname = regexp.$1;
Browser.version = regexp.$2;
else if (/version\d+ (\d[\d.) *). *safari/.test (useragent)) {//Safari
Browser.safari = true;
Browser.appname = ' Safari ';
Browser.version = regexp.$2;
}
return browser.appname + browser.version;
}

Object/Feature Detection method

This method is a common method for judging browser capabilities rather than the exact model of browsers. Most JS experts think this method is the most appropriate, because they think the script written by this method will stand the test of the future.

Get the version number of IE browser
Returns a value that shows the major version number of IE
function Getiever () {
Get client Information
var B = ua.indexof (///detection of the location of the special string "MSIE" )
if (b < 0) {
  return 0;
}
Return parsefloat (ua.substring (b + 5, Ua.indexof) (//Intercept version number string and convert to numeric value
}
 
  
return value 8 (my IE8)

You can use this approach if you are more concerned about the capabilities of your browser than the actual identity.

User-agent String Detection method

The User-agent string provides a large amount of information about a Web browser, including the name and version of the browser.

Get client Information
var info = {
ie:/msie/.test (UA) &&!/opera/.test (UA),  //matching IE browser
OP:/opera/.test (UA),  //matching Opera browser
SA:/version.*safari/.test (UA),  ///Match Safari browser
Ch:/chrome/.test (UA),  //matching Chrome browser
FF:/gecko/.test (UA) &&!/webkit/.test (UA)  //matching Firefox browser
};
(info.ie) && alert ("IE browser");
(INFO.OP) && alert ("Opera browser");
(INFO.SA) && alert ("Safari browser");
(INFO.FF) && alert ("Firefox browser");
(info.ch) && alert ("Chrome browser");

Usually the most we do is to determine whether IE, the other browsers will generally meet the standard. Some customers are satisfied with IE and FF only. Then we can do this:

"Microsoft Internet Explorer");

To judge ie far more than one method, you can use IE more specific things, such as window. Activexobject,document.all and so on, these are the object/feature detection Method! Usually you have to write different styles in different browsers (because IE style parsing is different), you have to judge the version. You can do this.

Get the version number of IE browser
Returns a value that shows the major version number of IE
function Getiever () {
Get client Information
var B = ua.indexof (///detection of the location of the special string "MSIE" )
if (b < 0) {
  return 0;
}
Return parsefloat (ua.substring (b + 5, Ua.indexof) (//Intercept version number string and convert to numeric value
}
 
  
return value 7

Detection operating system:

var Iswin = (navigator.userAgent.indexOf (///If it is a Windows system, returns True
var Ismac = (navigator.userAgent.indexOf (////If is Macintosh system, returns True
var Isunix = (navigator.userAgent.indexOf (///If UNIX system, returns True
var islinux = (navigator.userAgent.indexOf (////If Linux system, returns True

Most of the text comes from the JavaScript journey

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.