This article mainly introduces how to use javascript to determine the type and version of the current browser. Although it is not comprehensive, it is recommended for you to simply learn the methods and ideas. I wrote a method to determine the current browser type and version, and tested it only on IE 8/11, Google, and 360 browsers (not completely ).
I hope you will give your comments.
; (Function ($, window, document, undefined) {if (! Window. browser) {var userAgent = navigator. userAgent. toLowerCase (), uaMatch; window. browser = {}/*** determines whether it is ie */function isIE () {return ("ActiveXObject" in window );} /*** determine whether it is a Google browser */if (! UaMatch) {uaMatch = userAgent. match (/chrome \/([\ d.] +)/); if (uaMatch! = Null) {window. browser ['name'] = 'chrome '; window. browser ['version'] = uaMatch [1] ;}/ *** determines whether the Firefox browser is used */if (! UaMatch) {uaMatch = userAgent. match (/firefox \/([\ d.] +)/); if (uaMatch! = Null) {window. browser ['name'] = 'Firefox '; window. browser ['version'] = uaMatch [1] ;}/ *** determines whether it is an operabrowser */if (! UaMatch) {uaMatch = userAgent. match (/opera. ([\ d.] +)/); if (uaMatch! = Null) {window. browser ['name'] = 'Opera '; window. browser ['version'] = uaMatch [1] ;}/ *** determines if it is a Safari browser */if (! UaMatch) {uaMatch = userAgent. match (/safari \/([\ d.] +)/); if (uaMatch! = Null) {window. browser ['name'] = 'safari '; window. browser ['version'] = uaMatch [1] ;}}/*** determines whether it is IE */if (! UaMatch) {if (userAgent. match (/msie ([\ d.] + )/)! = Null) {uaMatch = userAgent. match (/msie ([\ d.] +)/); window. browser ['name'] = 'ie'; window. browser ['version'] = uaMatch [1];} else {/*** IE10 */if (isIE ()&&!! Document. attachEvent & (function () {"use strict"; return! This;} () {window. browser ['name'] = 'ie'; window. browser ['version'] = '10';}/*** IE11 */if (isIE ()&&! Document. attachEvent) {window. browser ['name'] = 'ie'; window. browser ['version'] = '11' ;}}/ *** registration Judgment Method */if (! $. IsIE) {$. extend ({isIE: function () {return (window. browser. name = 'ie') ;}}) ;}if (! $. IsChrome) {$. extend ({isChrome: function () {return (window. browser. name = 'chrome ') ;}}) ;}if (! $. IsFirefox) {$. extend ({isFirefox: function () {return (window. browser. name = 'Firefox ') ;}}) ;}if (! $. IsOpera) {$. extend ({isOpera: function () {return (window. browser. name = 'Opera ') ;}}) ;}if (! $. IsSafari) {$. extend ({isSafari: function () {return (window. browser. name = 'safari ') ;}}}}) (jQuery, window, document );
// Usage
console.log(window.browser);console.log($.isIE());console.log($.isChrome());
The above is all the content of this article. I hope you will like it.