Tip: Detecting a Mobile Browser with Javascript

There are a lot of existing ways to detect whether the browser is being used in a mobile device. You can use Modernizr to check for Touch support, check for whether Touch events are supported by the browser, or check the width of the screen. We previously used a combination of these, but they only worked most of the time.

Mozilla has recommended a better and shorter way to detect a mobile device and it is much more stable and reliable so far:

In summary, we recommend looking for the string “Mobi” anywhere in the User Agent to detect a mobile device.

if ( navigator.userAgent.match(/Mobi/) ) {
    // We are in a mobile device

Leave a Reply

Your email address will not be published. Required fields are marked *