Skip to content

pure-js/browser-detection

Repository files navigation

Detect browser

Build Status Test Coverage codecov

Browser detection using the user agent. Tested on most popular browser in the World on October 2017 (and updated on July 2023), statistic gets from StatCounter.

It's worth re-iterating: it's very rarely a good idea to use user agent sniffing. You can almost always find a better, more broadly compatible way to solve your problem!

More details here

Installation

npm i browser-version-detection --save

Script returns object with browser name and version, for example:

const output = {
  name: "Chrome",
  version: 62,
};

API

Get browser name and version

import { detectBrowser } from "browser-version-detection";

detectBrowser(window.navigator);

output

const output = {
  name: "Chrome",
  version: 64,
};

Get browser name

import { detectBrowserName } from "browser-version-detection";

detectBrowserName(window.navigator.userAgent);

output

const output = "Chrome";

Get browser version

import {
  detectBrowserName,
  detectBrowserVersion,
} from "browser-version-detection";

const name = detectBrowserName(window.navigator.userAgent);
detectBrowserVersion(window.navigator, name);

output

const output = 64;

Minified script located in dist/ folder

Contribution

pnpm i
pnpm start

Test

pnpm test

About

Browser detection by user agent, returns browser name and version

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published