I want os name, os version, browser name, browser version and device from the user agent. How can I use user agent parser js in my application in angular 4. I tried [email protected] but it doent return me the os version and device. Please help me
I want os name, os version, browser name, browser version and device from the user agent. How can I use user agent parser js in my application in angular 4. I tried [email protected] but it doent return me the os version and device. Please help me
Share Improve this question asked Feb 28, 2019 at 12:15 DonDon 371 silver badge4 bronze badges2 Answers
Reset to default 5To add it to package.json:
npm install ua-parser-js
To use it in your ponents or services you need an import for it to work:
import { UAParser } from 'ua-parser-js';
let parser = new UAParser();
console.log(parser.getResult());
I found the missing import in this other SO answer. Then you can use the documentation to get whatever information you're looking for.
The UAParser.js documentation is your best ally.
You can get OS name and version, browser name and other information using UAParser.js like that:
var parser = new UAParser();
// by default it takes ua string from current browser's window.navigator.userAgent
console.log(parser.getResult());
var result = parser.getResult();
console.log(result.browser); // {name: "Chromium", 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"