Javascript to identify different browsers Based on userAgent-javascript skills

Source: Internet
Author: User
This section describes how to identify different browsers in js Based on userAgent. If you need it, you can refer to the browser detection module. Note that the browser judgment sequence is mainly based on userAgent.

// Check the browser var client = function () {var engine = {ie: 0, gecko: 0, webkit: 0, khtml: 0, opera: 0, ver: null }; var browser = {// viewer ie: 0, firefox: 0, safari: 0, konq: 0, opera: 0, chrome: 0, ver: null}; var ua = navigator. userAgent; // the browser checks if (window. opera) {// opera disguise, so the engine is checked first. ver = browser. ver = window. opera. version (); engine. opera = browser. opera = parseFloat (engine. ver);} else if (/AppleWebKit \/(\ S + )/. test (ua) {engine. ver = RegExp ["$1"]; engine. webkit = parseFloat (engine. ver); // determine whether it is Chrome or Safariif (/Chrome \/(\ S + )/. test (ua) {browser. ver = RegExp ["$1"]; browser. chrome = parseFloat (browser. ver);} else if (/Version \/(\ S + )/. test (ua) {browser. ver = RegExp ["$1"]; browser. safari = parseFloat (browser. ver);} else {// approximately determines the version number var safariVersion = 1; if (engine. webkit <100) {safariVersion = 1;} else if (engine. webkit <312) {safariVersion = 1.2;} else if (engine. webkit <412) {safariVersion = 1.3;} else {safariVersion = 2;} browser. safari = browser. ver = safariVersion;} else if (/KHTML \/(\ S + )/. test (ua) |/Konqueror \/([^;] + )/. test (ua) {engine. ver = browser. ver = RegExp ["$1"]; engine. khtml = browser. konq = parseFloat (engine. ver);} else if (/rv :( [^ \)] +) \) Gecko \/\ d {8 }/. test (ua) {engine. ver = RegExp ["$1"]; engine. gecko = parseFloat (engine. ver); // determine if it is Firefoxif (/Firefox \/(\ S + )/. test (ua) {browser. ver = RegExp ["$1"]; browser. firefox = parseFloat (browser. ver) ;}} else if (/MSIE ([^;] + )/. test (ua) {engine. ver = browser. ver = RegExp ["$1"]; engine. ie = browser. ie = parseFloat (engine. ver);} return {engine: engine, browser: browser };} (); // call if (client. engine. webkit) {// if it's WebKit if (client. browser. chrome) {// execute code for Chrome} else if (client. browser. safari) {// run the code for Safari} else if (client. engine. gecko) {if (client. browser. firefox) {// execute code for Firefox} else {// execute code for other Gecko browsers }}
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.