Files
30-seconds-of-code/snippets/detectDeviceType.md
2022-06-08 12:30:49 +03:00

626 B

title, tags, expertise, cover, firstSeen, lastUpdated
title tags expertise cover firstSeen lastUpdated
Detect device type browser,regexp intermediate blog_images/clutter-2.jpg 2017-12-24T09:39:23+02:00 2020-10-22T20:23:47+03:00

Detects whether the page is being viewed on a mobile device or a desktop.

  • Use a regular expression to test the Navigator.userAgent property to figure out if the device is a mobile device or a desktop.
const detectDeviceType = () =>
  /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(
    navigator.userAgent
  )
    ? 'Mobile'
    : 'Desktop';
detectDeviceType(); // 'Mobile' or 'Desktop'