JS to determine browser version and browser kernel method _javascript Tips

Source: Internet
Author: User

This article is an example of how JS determines the browser version and the browser kernel. Share to everyone for your reference. The implementation methods are as follows:

Copy Code code as follows:

if (!browser.ie &&!browser.mac) {
var UA = Navigator.userAgent.toLowerCase (). toString ();
To determine if the IE kernel is not a version of IE
if ((Ua.indexof (' 360ee ') >-1) | | (Ua.indexof (' 360se ') >-1) | | (Ua.indexof (' SE ') >-1) | | (Ua.indexof (' aoyou ') >-1)
|| (Ua.indexof (' TheWorld ') >-1) | | (Ua.indexof (' Worldchrome ') >-1) | | (Ua.indexof (' Greenbrowser ') >-1)
|| (Ua.indexof (' Baidu ') >-1) | | (Ua.indexof (' Qqbrowser ') >-1)) {
Yes, switch compatibility mode.
window.open ("publicpage/point-se.aspx");
}
else {
If not, we recommend that you change your browser
Alert (' proposed switch to IE kernel browser ');
}
}
else {
Determine the version model of IE
if ((browser.version = && Browser.ie10compat) | | (browser.version = && browser.ie11compat)) {
window.open ("publicpage/point.aspx");
}
/*
* @desc to determine the browser version and the browser kernel
* @author wangyanling
* @date July 4, 2014
*/
var browser = function () {
var agent = Navigator.userAgent.toLowerCase (),
Opera = Window.opera,
Browser = {
Detects if the current browser is IE
ie:/(MSIE\S|TRIDENT.*RV:) ([\w.] +)/.test (agent),

Detects if the current browser is opera
Opera: (!!!) Opera && opera.version),

Detects if the current browser is a WebKit kernel browser
WebKit: (Agent.indexof (' applewebkit/') >-1),

Detects if the current browser is running under the Mac platform
Mac: (Agent.indexof (' Macintosh ') >-1),

Detects if the current browser is under "weird mode"
Quirks: (Document.compatmode = = ' Backcompat ')
};

Detects if the current browser kernel is a gecko kernel
Browser.gecko = (Navigator.product = = ' Gecko ' &&!browser.webkit &&!browser.opera &&! browser.ie);

var version = 0;

Internet Explorer 6.0+
if (browser.ie) {
var V1 = Agent.match (/(?: msie\s) ([\w.] +))/);
var v2 = Agent.match (/(?: Trident.*rv: ([\w.] +))/);
if (v1 && v2 && v1[1] && v2[1]) {
Version = Math.max (v1[1] * 1, v2[1] * 1);
else if (v1 && v1[1]) {
Version = v1[1] * 1;
else if (v2 && v2[1]) {
Version = v2[1] * 1;
} else {
Version = 0;
}

Detect if Browser mode is IE11 compatibility mode
Browser.ie11compat = Document.documentmode = = 11;

Detect if Browser mode is IE9 compatibility mode
Browser.ie9compat = Document.documentmode = = 9;

Detect if Browser mode is IE10 compatibility mode
Browser.ie10compat = Document.documentmode = = 10;

Detect if the browser is a IE8 browser
BROWSER.IE8 =!! Document.documentmode;

Detect if Browser mode is IE8 compatibility mode
Browser.ie8compat = Document.documentmode = = 8;

Detect if Browser mode is IE7 compatibility mode
Browser.ie7compat = ((Version = = 7 &&!document.documentmode) | | document.documentmode = = 7);

Detect if Browser mode is IE6 mode or weird mode
Browser.ie6compat = (Version < 7 | | browser.quirks);

Browser.ie9above = version > 8;

Browser.ie9below = version < 9;
}

Gecko.
if (Browser.gecko) {
var geckorelease = Agent.match (/rv: ([\d\.] +)/);
if (geckorelease) {
Geckorelease = Geckorelease[1].split ('. ');
Version = Geckorelease[0] * 10000 + (Geckorelease[1] | | 0) * + (geckorelease[2) | | 0) * 1;
}
}

Detects if the current browser is chrome and, if so, returns the large version of Chrome
if (/chrome\/(\d+\.\d)/i.test (agent)) {
Browser.chrome = +regexp[' \x241 '];
}

Detects if the current browser is safari and, if so, returns the large version number of Safari
if (/(\d+\.\d)? (?: \. \d)? \s+safari\/? (\d+\.\d+)?/i.test (Agent) &&!/chrome/i.test (agent)) {
Browser.safari = + (regexp[' \x241 ') | | regexp[' \x242 ']);
}

Opera 9.50+
if (Browser.opera)
Version = Parsefloat (Opera.version ());

WebKit 522+ (Safari 3+)
if (Browser.webkit)
Version = parsefloat (Agent.match (/applewebkit\/(\d+)/) [1]);

Detects the current browser version number
Browser.version = version;

return browser;
}();

The

wants this article to help you with your JavaScript programming.

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.