Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript: Checking if user agent is on desktop device

I'm looking all over the web and still can't find a pretty decent way to check if a browser is used on a desktop computer or on tablet/mobile.

Consider the next issue, there are touch devices which are desktop (desktop surface). Eventually what im trying to achieve is to separate between the next two: 1. Touch devices that have orientation switching enabled 2. Touch devices that are actually desktop ones

Is there any recommended way to check this?

Also, I tried the next 3rd party WURFL.io but i found 2 issues with that: It gives you a too-simple result (which encapsulates all the decisions inside) and it basically not working well when emulating a mobile device on chrome.

like image 478
Popokoko Avatar asked Feb 12 '23 07:02

Popokoko


1 Answers

You can use

if (navigator.userAgent.match(/(iPhone|iPod|iPad|Android|BlackBerry|IEMobile)/)) {
  dosomething();
} else {
   dosomething(); //this is the browser
}
like image 140
sagar43 Avatar answered Feb 13 '23 20:02

sagar43