Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easiest way to determine if user is on mobile device

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?

like image 704
RailsTweeter Avatar asked Mar 12 '13 15:03

RailsTweeter


3 Answers

Check this http://detectmobilebrowsers.com/

Work for Javascript, jQuery etc.

like image 185
injetkilo Avatar answered Oct 21 '22 23:10

injetkilo


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;
like image 9
Rob M. Avatar answered Oct 21 '22 23:10

Rob M.


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.

like image 6
Jonathan Potter Avatar answered Oct 21 '22 23:10

Jonathan Potter