Javascript code used to determine the browser version and the browser kernel _ javascript skills

Source: Internet
Author: User
This article mainly introduces how to judge the browser version and kernel of the browser by js, which can be used for judgment on various browsers and has some reference value, for more information about how to determine the browser version and the browser kernel, see the examples in this article. Share it with you for your reference. The specific implementation method is as follows:

The Code is as follows:


If (! Browser. ie &&! Browser. mac ){
Var UA = navigator. userAgent. toLowerCase (). toString ();
// Determine whether it is a non-IE version in the IE Kernel
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 the compatible mode.
Window. open ("publicPage/point-se.aspx ");
}
Else {
// If not, we recommend that you change the browser
Alert ('browser recommended for IE kernels ');
}
}
Else {
// Determine the version and model of IE
If (browser. version = 10 & browser. ie10Compat) | (browser. version = 11 & browser. ie11Compat )){
Window. open ("publicPage/point. aspx ");
}
/*
* @ Desc determine the browser version and the browser kernel
* @ Author wangyanling
* @ Date January 1, July 4, 2014
*/
Var browser = function (){
Var agent = navigator. userAgent. toLowerCase (),
Opera = window. opera,
Browser = {
// Check whether the current browser is IE
Ie:/(msie \ s | trident. * rv :) ([\ w.] +)/. test (agent ),

// Check whether the current browser is Opera
Opera :(!! Opera & opera. version ),

// Check whether the current browser is a webkit kernel Browser
Webkit: (agent. indexOf ('applewebkit/')>-1 ),

// Check whether the current browser is running on the mac Platform
Mac: (agent. indexOf ('macintosh ')>-1 ),

// Check whether the current browser is in "weird mode"
Quirks: (document. compatMode = 'background ')
};

// Check whether 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;
}

// Check whether the browser mode is IE11 compatible
Browser. ie11Compat = document.doc umentMode = 11;

// Check whether the browser mode is IE9 compatible
Browser. ie9Compat = document.doc umentMode = 9;

// Check whether the browser mode is IE10 compatible
Browser. ie10Compat = document.doc umentMode = 10;

// Check whether the browser is an Internet Explorer 8.
Browser. ie8 = !! Document.doc umentMode;

// Check whether the browser mode is IE8 compatible
Browser. ie8Compat = document.doc umentMode = 8;

// Check whether the browser mode is IE7 compatible
Browser. ie7Compat = (version = 7 &&! Document.doc umentMode) | document.doc umentMode = 7 );

// Check whether the browser mode is IE6 or weird
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) * 100 + (geckoRelease [2] | 0) * 1;
}
}

// Check whether the current browser is Chrome. If yes, the large version of Chrome is returned.
If (/chrome \/(\ d + \. \ d)/I. test (agent )){
Browser. chrome = + RegExp ['\ x241'];
}

// Check whether the current browser is Safari. If yes, the Safari version is returned.
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]);

// Check the current browser version
Browser. version = version;

Return browser;
}();

I hope this article will help you design javascript programs.

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.