JavaScript writes code samples to detect browsers used by users _javascript tips

Source: Internet
Author: User

Capability Testing
the ability to detect a particular browser before writing code. For example, a script might have to detect the first payment of a function before calling it. This method frees developers from considering specific browser types and versions, allowing them to focus on whether the corresponding capabilities exist. Ability detection does not accurately detect specific browsers and versions.

Eccentricity detection
The quirk is actually a bug in the browser implementation, such as an eccentricity in the early webkit that it returns the hidden attribute in the for-in loop. Quirk detection usually involves running a piece of code and then determining whether the browser has a quirk. Because eccentricity detection does not accurately detect specific browsers and versions.

User Agent Detection
identifies the browser by detecting the user agent string. The user agent string contains a large number of browser-related information, including browsers, platforms, operating systems, and browser versions. The user agent string has a fairly long history of development, during which the browser provider view, by adding deceptive information to the user agent string, deceives the Web site in detail with its own browser. User agent detection requires special techniques, especially if opera hides its user agent string. Even so, the user agent string is still able to detect the rendering engine used by the browser and the platform on which it resides, including mobile devices and gaming systems.

During each HTTP request, the user agent string is sent as the first response, and the string can be accessed through the Navigator.useragent property of JavaScript. On the server side, it is a common and widely accepted practice to determine which browsers users use by detecting user agent strings. On the client side, user-agent detection is generally regarded as a last resort, with priority ranked after capacity testing and eccentricity testing.

var client = function () {//render engine var engine = {ie:0, gecko:0, webkit:0, khtml:0, opera:0,//full version number ver

 : null}; Browser var browser = {//main browser ie:0, firefox:0, safari:0, konq:0, opera:0, chrome:0,//specific version number Ver:nu

 LL};
 Detection rendering engine and 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["$"];

  Engine.webkit = parsefloat (engine.ver);
   Determine if Chrome or Safari if (/chrome\/(\s+)/.test (UA)) {Browser.ver = regexp["$"];
  Browser.chrome = parsefloat (engine.ver);
   else if (/version\/(s+)/.test (UA)) {Browser.ver = regexp["$"];
  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["$"];
 engine.khtml = Browser.kong = parsefloat (engine.ver);
  else if (/rv: ([^\)]+) \) gecko\/\d{8}/.test (UA)) {Engine.ver = regexp["$"];

  Engine.gecko = parsefloat (engine.ver);
   Determine if the Firefox if (/firefox\/(s+)/.test (UA)) {Browser.ver = regexp["$"];
  Browser.firefox = parsefloat (browser.ver); } else if (/msie ([^;]
  +)/.test (UA)) {engine.ver = Browser.ver = regexp["$"];

 engine.ie = browser.ie = parsefloat (engine.ver);
 }//Detect browser browser.ie = engine.ie;

 Browser.opera = Engine.opera;

Returns these objects return {engine:engine, browser:browser}} ();
Console.log (Client.engine);

 Console.log (Client.browser);

Tangram Detect browser source code

/** * Statement Baidu Package/var Baidu = Baidu | | {version: "1-3-2"}; Meizz 20100513 upgrades the GUID to \x06 baidu.guid = "$BAIDU $";//Raise the GUID to prevent modification window[undefined] 20100504 Berg/** * Meizz 2010/0 2/04 * Top-level domain name Baidu may be hijacked by closures, and the need for page-level unique information needs to use the following object * * Window[baidu.guid] = Window[baidu.guid] | |

{}; /** * Declaration Baidu.browser Package * * * baidu.browser = Baidu.browser | |

{}; /** * To determine if Isgecko/Baidu.browser.isGecko =/gecko/i.test (navigator.useragent) &&!/like gecko/i.test (navigat

Or.useragent);

/** * Judge whether it is iswebkit * * * Baidu.browser.isWebkit =/webkit/i.test (navigator.useragent);

/** * To determine whether the standard mode * * Baidu.browser.isStrict = Document.compatmode = = "Css1compat"; /** * To determine if it is a Safari browser/if ((\d+\.\d) (\.\d) \s+safari/i.test (navigator.useragent) &&!/chrome/i.test (

navigator.useragent))) {Baidu.browser.safari = parsefloat (regexp[' \x241 ')); /** * To determine whether Opera browser/if (/opera\/(\d+\.\d)/i.test (navigator.useragent)) {Baidu.browser.opera = parsefloat (regexp[' \x
241 ']); }/** * Determines whetherFor Chrome browser/if (/chrome\/(\d+\.\d)/i.test (navigator.useragent)) {baidu.browser.chrome = parsefloat (regexp[' \x241 ')
); /** * Determine if it is IE browser/if (/msie (\d+\.\d)/i.test (navigator.useragent)) {baidu.ie = baidu.browser.ie = Document.docume Ntmode | |
parsefloat (regexp[' \x241 ')); /** * Determine if it is a Firefox browser/if (/firefox\/(\d+\.\d)/i.test (navigator.useragent)) {Baidu.browser.firefox = parsefloat (R
 egexp[' \x241 ']);
 ' \x241 ' is the octal notation ' \x24 ' corresponds to the character ' $ ', so ' \x241 ' equals ' $ '}

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.