Skip to main content

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โ€‹

APIChromeEdgeFirefoxSafariiOS SafariBaseline
Navigator.userAgentโœ… 1+โœ… 12+โœ… 1+โœ… 1+โœ… 1+Widely available
Navigator.languageโœ… 1+โœ… 12+โœ… 1+ [1]โœ… 1+โœ… 1+Widely available
  1. 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 โ€” true if running in a browser environment
  • getUserAgent() โ€” the navigator.userAgent string, or undefined
  • isMobile() โ€” true if the user agent matches a mobile device
  • getBrowserLanguage() โ€” the navigator.language string, or undefined
  • getPlatform() โ€” the OS as a Platform ('ios' | 'android' | 'macos' | 'windows' | 'linux' | 'unknown'); prefers navigator.userAgentData, falls back to the user agent, and reports iPadOS as 'ios'
  • isIOS() โ€” true on iOS or iPadOS
  • isAndroid() โ€” true on Android

See the API reference for full signatures.