Easiest way to determine if user is on mobile device

28,202

Solution 1

Check this http://detectmobilebrowsers.com/

Work for Javascript, jQuery etc.

Solution 2

A user agent check is the "easiest", though you could easily employ CSS3 media queries

Here is an example that checks iphone, android and blackberry; you could easily add other mobile browsers.

var is_mobile = !!navigator.userAgent.match(/iphone|android|blackberry/ig) || false;

Solution 3

I find that it's best to use feature detection. Use Modernizr to detect if it's a touch device. You can do things like:

var mousedown = 'mousedown';

if (Modernizr.touch) {
    mousedown = 'touchstart';
}

$('.foo').on(mousedown, handleMouseDown);

And then use CSS Media Queries for handling screen width (and it's also easy to detect screen width with javascript). That way you can correctly handle touch devices with large screens, or non-touch devices with small screens.

Share:
28,202
RailsTweeter
Author by

RailsTweeter

Updated on September 18, 2020

Comments

  • RailsTweeter
    RailsTweeter over 3 years

    I'm showing a notification bar on my website, and frankly, it doesn't work well when its on a mobile device. I'd like to show the bar ONLY for desktop users.

    What is the easiest way to determine if a user is on desktop or on mobile?