common
Small environment and user-agent checks used to detect whether code is running in a browser, on mobile, and what language/agent and OS platform the browser reports.
Import: @rtorcato/browser-common/common
๐ MDN: Navigator
Browser supportโ
| API | Chrome | Edge | Firefox | Safari | iOS Safari | Baseline |
|---|---|---|---|---|---|---|
Navigator.userAgent | โ 1+ | โ 12+ | โ 1+ | โ 1+ | โ 1+ | Widely available |
Navigator.language | โ 1+ | โ 12+ | โ 1+ [1] | โ 1+ | โ 1+ | Widely available |
- Firefox: Before Firefox 4, this property's value was also part of the user agent string, as reported by navigator.userAgent. Starting in Firefox 5, this property's value is based on the value of the Accept-Language HTTP header.
โ
supported ยท โ partial ยท โ not supported ยท [n] see note. Generated from MDN browser-compat-data and web-features. caniuse: Navigator.userAgent ยท Navigator.language
Exampleโ
import {
isBrowser,
getUserAgent,
isMobile,
getBrowserLanguage,
getPlatform,
isIOS,
isAndroid,
} from '@rtorcato/browser-common/common'
if (isBrowser) {
console.log(getUserAgent(), isMobile(), getBrowserLanguage())
console.log(getPlatform(), isIOS(), isAndroid())
}
Exportsโ
isBrowserโtrueif running in a browser environmentgetUserAgent()โ thenavigator.userAgentstring, orundefinedisMobile()โtrueif the user agent matches a mobile devicegetBrowserLanguage()โ thenavigator.languagestring, orundefinedgetPlatform()โ the OS as aPlatform('ios' | 'android' | 'macos' | 'windows' | 'linux' | 'unknown'); prefersnavigator.userAgentData, falls back to the user agent, and reports iPadOS as'ios'isIOS()โtrueon iOS or iPadOSisAndroid()โtrueon Android
See the API reference for full signatures.