JavaScript code example for detecting the browser used by users, javascript example

Source: Internet
Author: User

JavaScript code example for detecting the browser used by users, javascript example

Capability Detection
Check the capabilities of a specific browser before writing the code. For example, before a script calls a function, it may need to check that the function has a down payment. This detection method frees developers from considering specific browser types and versions, so that they can focus on the existence of corresponding capabilities. Capability detection cannot accurately detect specific browsers and versions.

Quirks Detection
Quirks are actually bugs in browser implementation. for example, early webkit has a quirk, that is, it will return hidden attributes in the for-in loop. Geek detection usually involves running a piece of code and then determining whether a browser has a geek. Since the geek detection cannot accurately detect specific browsers and versions.

User Agent Detection
The browser is identified by detecting the user proxy string. The user agent string contains a large amount of information related to the browser, including the browser, platform, operating system, and browser version. The user proxy string has a long history. During this period, the browser provider view adds some deceptive information to the user proxy string to cheat the website details. User proxy detection requires special skills, especially when Opera conceals the user proxy string. Even so, the user agent string can still detect the rendering engine used by the browser and its platform, including mobile devices and game systems.

During each HTTP request, the user agent string is sent as the response header and can be accessed through the navigator. userAgent attribute of Javascript. On the server side, it is a common and widely accepted method to detect the user's browser by detecting the user proxy string. On the client side, user agent detection is generally treated as a last resort. Its priority is placed after capability detection and quirks detection.

Var client = function () {// rendering engine var engine = {ie: 0, gecko: 0, webkit: 0, khtml: 0, opera: 0, // complete version: ver: null}; // browser var browser = {// main browser ie: 0, firefox: 0, safari: 0, konq: 0, opera: 0, chrome: 0, // The specific version number ver: null}; // checks the rendering engine and the browser var ua = navigator. userAgent; if (window. opera) {engine. 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 Safari if (/Chrome \/(\ S + )/. test (ua) {browser. ver = RegExp ["$1"]; browser. chrome = parseFloat (engine. 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. kong = parseFloat (engine. ver);} else if (/rv :( [^ \)] +) \) Gecko \/\ d {8 }/. test (ua) {engine. ver = RegExp ["$1"]; engine. gecko = parseFloat (engine. ver); // determine if it is firefox if (/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);} // check the browser. ie = engine. ie; browser. opera = engine. opera; // return these objects return {engine: engine, browser: browser }}(); console. log (client. engine); console. log (client. browser );

Tangram detection browser source code

/*** Declare the baidu package */var baidu = baidu | {version: "1-3-2"}; // meizz 20100513 upgrade the guid to \ x06baidu. guid = "$ BAIDU $"; // proposes a guid to prevent the top-level domain name baidu from being hijacked by the closure by modifying window [undefined] 20100504 berg/*** meizz 2010/02/04, the following object */window [baidu is required for page-level unique information. guid] = window [baidu. guid] |{};/*** declares baidu. browser package */baidu. browser = baidu. browser | {};/*** determine whether it is isGecko */baidu. browser. isGecko =/gecko/I. test (navigato R. userAgent )&&! /Like gecko/I. test (navigator. userAgent);/*** determine whether it is isWebkit */baidu. browser. isWebkit =/webkit/I. test (navigator. userAgent);/*** determine whether the mode is standard */baidu. browser. isStrict = document. compatMode = "CSS1Compat";/*** determine whether the browser is safari */if (/(\ d + \. \ d )(\. \ d )? \ S + safari/I. test (navigator. userAgent )&&! /Chrome/I. test (navigator. userAgent) {baidu. browser. safari = parseFloat (RegExp ['\ x241']);}/*** determine whether it is an operabrowser */if (/opera \/(\ d + \. \ d)/I. test (navigator. userAgent) {baidu. browser. opera = parseFloat (RegExp ['\ x241']);}/*** determines whether it is a chrome browser */if (/chrome \/(\ d ++ \. \ d)/I. test (navigator. userAgent) {baidu. browser. chrome = parseFloat (RegExp ['\ x241']);}/*** determines whether it is an IE browser */if (/msie (\ d ++ \. \ d)/I. test (navigator. userAgent) {baidu. ie = baidu. browser. ie = document.doc umentMode | parseFloat (RegExp ['\ x241']);} /*** determine if it is a firefox browser */if (/firefox \/(\ d + \. \ d)/I. test (navigator. userAgent) {baidu. browser. firefox = parseFloat (RegExp ['\ x241']); // '\ x241' is the octal notation' \ x24' corresponding to '$ ', so '\ x241' is equivalent to' $1 '}

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.