how to detect device javascirpt

how to detect device javascirpt

To detect the device type (e.g., mobile, tablet, desktop) in JavaScript, you can use the navigator.userAgent property. This property returns a string that represents the user agent of the browser, which can be used to identify the device and its capabilities.

Here's an example of how to use the navigator.userAgent property to detect the device type:

refer ‮ttual:ot‬uri.com
if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) {
  // Device is a mobile or tablet
} else {
  // Device is a desktop
}

The above code uses a regular expression to test the user agent string against a list of known mobile and tablet devices. If the user agent string matches any of the listed devices, the device is considered a mobile or tablet. Otherwise, it is considered a desktop.

Note that this method is not foolproof, as user agents can be modified or spoofed. It is recommended to use a library or service that provides more reliable device detection, such as the W3C Device Detection API.

Created Time:2017-11-01 20:42:49  Author:lautturi