Home

Awesome

detector


NPM version Build Status Coverage Status

中文文档

Client information detector by user agent, detect information include:

  1. Device.
  2. Operation System (OS).
  3. Browser.
  4. Rendering Engine.

Detected information is a "detector object", data structure like:

detector = {
    device: {
        name: "iphone",
        version: -1,
        fullVersion: "-1",
        [iphone]: -1
    },
    os: {
        name: "ios",
        version: 6.1,
        fullVersion: "6.1",
        [ios]: 6.1
    },
    browser: {
        name: "chrome":
        version: 26.0,
        fullVersion: "26.0.1410.50",
        mode: 26.0,
        fullMode: "26.0.1410.50",
        compatible: false,
        [chrome]: 26.0
    },
    engine: {
        name: "webkit",
        version: 536.26,
        fullVersion: "536.26",
        mode: 523.26,
        fullMode: "523.26",
        compatible: false,
        [webkit]: 536.26
    }
}

Note: Above [iphone], [ios], [chrome], [webkit] is dynamically from actual environment, different device, operation system, browser and rendering engine is different.

Note:

Installation

via npm:

Installation to global (with -g argument), you can use detector command in terminal.

npm install detector [-g]

Usage

for Web Browser

Some examples in common use:

// import detector, variable detector is "detector object".
const detector = require('detector');

// Detect browser name.
detector.browser.name === "chrome" // true

// An other example for detect browser name.
!!detector.browser.ie // false

// Detect the old browseres.
if(detector.browser.ie && detector.browser.version < 8){
    alert("You browser is too old.");
}

// Detect rendering engine below Trident 4 (IE8).
if(detector.engine.trident && detector.engine.mode < 4){
    // hack code.
}

// Collect client detail informations.
detector.browser.name + "/" + detector.browser.fullVersion;

for Node.js

const detector = require('detector');

// variable `d` is a "detector object"
const d = detector.parse(userAgent);
d.browser.name === "chrome";

API

{String} detector.device.name

Name of hardware device.

{Number} detector.device.version

Version of hardware device.

{String} detector.device.fullVersion

Full version of hardware device.

{Number} detector.device[device_name]

Detect name of hardware device.

Support hardware devices:

NODE ONLY

Following hardware device support in NodeJS version of detector:


{String} detector.os.name

Name of operation system.

{Number} detector.os.version

Version of operation system.

{String} detector.os.fullVersion

Full version of operation system.

{Number} detector.os[os_name]

Detect name of operation system.

Support operation system list:

NODE ONLY

Following operation system support in NodeJS version of detector:


{String} detector.browser.name

Name of browser.

{Number} detector.browser.version

Real version of browser.

In compatibility-mode, Internet Explorer declare it is a old browser. but detector.browser.version return the real version of browser.

For example:

IE9 declare it is a IE7 in compatibility-mode, but detector.browser.version return 9.0.

{String} detector.browser.fullVersion

Full (real) version of browser.

{Number} detector.browser.mode

Browser-mode. In Internet Explorer's compatibility-mode, version and mode is different.

{String} detector.browser.fullMode

Full mode of browser.

{Number} detector.browser[browser_name]

Detect name of browser.

Support browser list:

NODE ONLY

{Boolean} detector.browser.compatible

Judge is browser in compatibility-mode.


{String} detector.engine.name

Name of rendering engine.

{Number} detector.engine.version

Version of rendering engine.

{String} detector.engine.fullVersion

Full version of rendering engine.

{Number} detector.engine.mode

Mode of rendering engine.

{String} detector.engine.fullMode

Full-mode of rendering engine.

{Number} detector.engine[engine_name]

Detect name of rendering engine.

Support rendering engine list:

{detector} detector.parse(String ua)

Parse user agent string, return a detector object.


Not Available information:

Releases

https://github.com/hotoo/detector/tree/gh-pages