Search code examples
javascriptandroidiosuser-agent

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.


Solution

  • You can use

    if (navigator.userAgent.match(/(iPhone|iPod|iPad|Android|BlackBerry|IEMobile)/)) {
      dosomething();
    } else {
       dosomething(); //this is the browser
    }