Use javascript to determine the current browser _ javascript skills

Source: Internet
Author: User
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.

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.