Master UAParser.js: Quickly Detect Browser, OS, and Device Info

UAParser.js is a lightweight, dependency‑free JavaScript library that parses the User‑Agent string to provide detailed browser, engine, OS, CPU, and device information, with examples for both browser and Node.js environments, and outlines its main methods and installation steps.

Java High-Performance Architecture
Java High-Performance Architecture
Java High-Performance Architecture
Master UAParser.js: Quickly Detect Browser, OS, and Device Info

UAParser.js

Sometimes we need to determine client information such as browser type, model, etc., and handle accordingly; for example, if accessed via WeChat, do something.

This requires extracting information from the User-Agent, and UAParser.js is a professional User-Agent parsing library.

Main features:

Full functionality, easy to use

Encapsulated methods allow easy retrieval of browser type, layout engine, operating system, CPU type, and device type.

Works in browsers and node.js

Wide applicability: jQuery/Zepto plugins, Bower/Meteor packages, RequireJS/AMD modules.

Very small

Only 11Kb , and with gzip only 4Kb .

No dependencies required

Example

Browser usage

<script type="text/javascript" src="dist/ua-parser.min.js"></script>
<script type="text/javascript">
    var parser = new UAParser();
    var result = parser.getResult();
    
    console.log(result.browser);
    console.log(result.device);
    console.log(result.os);
    console.log(result.os.version);
    console.log(result.engine.name);
    console.log(result.cpu.architecture);
</script>

Node.js usage

Installation $ npm install ua-parser-js Usage

var http = require('http');
var parser = require('ua-parser-js');

http.createServer(function (req, res) {
    var ua = parser(req.headers['user-agent']);
    res.end(JSON.stringify(ua, null, '  '));
})
.listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');

Main methods

Get overall information

getResult()

Returns all information:

{
  ua: '',
  browser: {},
  cpu: {},
  device: {},
  engine: {},
  os: {}
}

getUA()

Gets the raw User-Agent string.

Get partial information

getBrowser()

{ name: '', version: '' }

getDevice()

{ model: '', type: '', vendor: '' }

getEngine()

{ name: '', version: '' }

getOS()

{ name: '', version: '' }

Conclusion

Although User-Agent parsing scenarios are limited, having an impression of UAParser.js helps quickly implement requirements when needed.

Project URL

https://github.com/faisalman/ua-parser-js
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaScriptNode.jsUser-AgentBrowser DetectionUAParser.js
Java High-Performance Architecture
Written by

Java High-Performance Architecture

Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.