Skip to content

Repository files navigation

Deprecated: use ua-parser-js instead

Lightweight JavaScript-based User-Agent string parser. Supports browser & node.js environment. Also available as Component package, Bower package, jQuery.ua, & AMD module

Build Status

Installation

$ component install component/user-agent-parser

Example

varparse=require('user-agent-parser');parse('user agent string')// => Object

Features

Extract detailed type of web browser, layout engine, operating system, and device purely from user-agent string with relatively lightweight footprint (~7KB minified / ~3KB gzipped). Written in vanilla js, which means it doesn't depends on any other library.

It's over 9000

Methods

  • getBrowser()
    • returns { name: '', major: '', version: '' }
# Possible 'browser.name':
Amaya, Arora, Avant, Baidu, Blazer, Bolt, Camino, Chimera, Chrome, Chromium,
Comodo Dragon, Conkeror, Dillo, Dolphin, Doris, Epiphany, Fennec, Firebird,
Firefox, Flock, GoBrowser, iCab, ICE Browser, IceApe, IceCat, Iceweasel,
IE [Mobile], Jasmine, K-Meleon, Konqueror, Kindle, Links, Lunascape, Lynx, Maemo,
Maxthon, Midori, Minimo, [Mobile] Safari, Mosaic, Mozilla, Netfront, Netscape,
NetSurf, Nokia, OmniWeb, Opera [Mini/Mobi/Tablet], Phoenix, Polaris, RockMelt,
Silk, Skyfire, SeaMonkey, SlimBrowser, Swiftfox, Tizen, UCBrowser, w3m, Yandex
# 'browser.version' & 'browser.major' determined dynamically
  • getDevice()
    • returns { model: '', type: '', vendor: '' }
# Possible 'device.type':
console, mobile, tablet
# Possible 'device.vendor':
Acer, Alcatel, Apple, Asus, BenQ, BlackBerry, Dell, GeeksPhone, HP, HTC, Huawei,
Lenovo, LG, Meizu, Motorola, Nexian, Nintendo, Nokia, Palm, Panasonic,
RIM, Samsung, Siemens, Sony-Ericsson, Sprint, ZTE
# 'device.model' determined dynamically
  • getEngine()
    • returns { name: '', version: '' }
# Possible 'engine.name'
Amaya, Gecko, iCab, KHTML, Links, Lynx, NetFront, NetSurf, Presto, Tasman,
Trident, w3m, WebKit
# 'engine.version' determined dynamically
  • getOS()
    • returns { name: '', version: '' }
# Possible 'os.name'
AIX, Amiga OS, Android, Arch, Bada, BeOS, BlackBerry, CentOS, Chromium OS,
Fedora, Firefox OS, FreeBSD, Debian, DragonFly, Gentoo, GNU, Haiku, Hurd, iOS,
Joli, Linux, Mac OS, Mandriva, MeeGo, Minix, Mint, Morph OS, NetBSD, Nintendo,
OpenBSD, OS/2, Palm, PCLinuxOS, Plan9, Playstation, QNX, RedHat, RIM Tablet OS,
RISC OS, Slackware, Solaris, SUSE, Symbian, Tizen, Ubuntu, UNIX, WebOS,
Windows [Phone/Mobile], Zenwalk
# 'os.version' determined dynamically
  • getCPU()
    • returns { architecture: '' }
# Possible 'cpu.architecture'
68k, amd64, arm, ia32, ia64, irix, mips, pa-risc, ppc, sparc
  • getResult()
    • returns { browser: {}, cpu: {}, device: {}, engine: {}, os: {} }
  • getUA()
    • returns UA string of current instance
  • setUA(uastring)
    • set & parse UA string

Example

<!doctype html><html><head><scripttype="text/javascript" src="ua-parser.min.js"></script><scripttype="text/javascript">varparser=newUAParser();// by default it takes ua string from current browser's window.navigator.userAgentconsole.log(parser.getResult());/* /// this will print an object structured like this: { browser: { name: "", version: "", major: "" }, engine: { name: "", version: "" }, os: { name: "", version: "" }, device: { model: "", type: "", vendor: "" }, cpu: { architecture: "" } } */// let's test a custom user-agent string as an examplevaruastring="Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.2 (KHTML, like Gecko) Ubuntu/11.10 Chromium/15.0.874.106 Chrome/15.0.874.106 Safari/535.2";parser.setUA(uastring);varresult=parser.getResult();// this will also produce the same result (without instantiation):// var result = UAParser(uastring);console.log(result.browser);// {name: "Chromium", major: "15", version: "15.0.874.106"}console.log(result.device);// {model: undefined, type: undefined, vendor: undefined}console.log(result.os);// {name: "Ubuntu", version: "11.10"}console.log(result.os.version);// "11.10"console.log(result.engine.name);// "WebKit"console.log(result.cpu.architecture);// "amd64"// do some other testsvaruastring2="Mozilla/5.0 (compatible; Konqueror/4.1; OpenBSD) KHTML/4.1.4 (like Gecko)";console.log(parser.setUA(uastring2).getBrowser().name);// "Konqueror"console.log(parser.getOS());// {name: "OpenBSD", version: undefined}console.log(parser.getEngine());// {name: "KHTML", version: "4.1.4"}varuastring3='Mozilla/5.0 (PlayBook; U; RIM Tablet OS 1.0.0; en-US) AppleWebKit/534.11 (KHTML, like Gecko) Version/7.1.0.7 Safari/534.11';console.log(parser.setUA(uastring3).getDevice().model);// "PlayBook"console.log(parser.getOS())// {name: "RIM Tablet OS", version: "1.0.0"}console.log(parser.getBrowser().name);// "Safari"</script></head><body></body></html>

Using node.js

$ npm install ua-parser-js
varUAParser=require('ua-parser-js');varparser=newUAParser();varua=request.headers['user-agent'];// user-agent header from an HTTP requestconsole.log(parser.setUA(ua).getResult());

Using requirejs

require(['ua-parser'],function(UAParser){varparser=newUAParser();console.log(parser.getResult());});

Using component

$ component install faisalman/ua-parser-js
varUAParser=require('ua-parser-js');varparser=newUAParser();console.log(parser.getResult());

Using bower

$ bower install ua-parser-js

Using jQuery.ua

Although written in vanilla js (which means it doesn't depends on jQuery), this library will automatically detect if jQuery is present and create $.ua object based on browser's user-agent (although in case you need, window.UAParser constructor is still present). To get/set user-agent you can use: $.ua.get() / $.ua.set(uastring).

// In browser with default user-agent: 'Mozilla/5.0 (Linux; U; Android 2.3.4; en-us; Sprint APA7373KT Build/GRJ22) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0':// Do some testsconsole.log($.ua.device);// {vendor: "HTC", model: "Evo Shift 4G", type: "mobile"}console.log($.ua.os);// {name: "Android", version: "2.3.4"}console.log($.ua.os.name);// "Android"console.log($.ua.get());// "Mozilla/5.0 (Linux; U; Android 2.3.4; en-us; Sprint APA7373KT Build/GRJ22) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0"// reset to custom user-agent$.ua.set('Mozilla/5.0 (Linux; U; Android 3.0.1; en-us; Xoom Build/HWI69) AppleWebKit/534.13 (KHTML, like Gecko) Version/4.0 Safari/534.13');// Test againconsole.log($.ua.browser.name);// "Safari"console.log($.ua.engine.name);// "Webkit"console.log($.ua.device);// {vendor: "Motorola", model: "Xoom", type: "tablet"}console.log(parseInt($.ua.browser.version.split('.')[0],10));// 4

Development

Install dependencies

$ npm install jshint
$ npm install mocha
$ npm install uglify-js

Verify, test, & minify script

$ ./build/build.sh

License

Dual licensed under GPLv2 & MIT

Copyright © 2012-2013 Faisalman <fyzlman@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

About

Lightweight JavaScript-based User-Agent string parser. Supports browser & node.js environment.

Resources

Stars

21 stars

Watchers

5 watching

Forks

Releases

Packages

Contributors

Languages