Detect if is mobile with Javascript, read user agent

How to detect if your user is using a mobile device in Javascript? You can do it by checking the…

November 5, 2013

How to detect if your user is using a mobile device in Javascript? You can do it by checking the user agent, that is the “signature” of the browser. When a browser request a page it introduces himself by telling which browser is. This signature is accessible by php on the server, but also by Javascript on the client.

Read User Agent in Javascript:

function isMobile () {
   if( navigator.userAgent.match(/iphone/i) ||
   navigator.userAgent.match(/ipad/i) ||
   navigator.userAgent.match(/android/i) ||
   navigator.userAgent.match(/blackberry/i) ||
   navigator.userAgent.match(/android/i)
   ) {
      return true;
   }
   return false;
}

This code search the user agent string for matching for keyword “ipad”, “iphone”, “blackberry” and “android”. If there is an occurence return true, else return false;

Author

I'm a software engineer, an everyday web developer and a maker. I usually build sites with PHP, within or without WordPress. I build Internet of Things with Arduino and ESP8266. I'm the founder of Rockit.it and Dailybest.it and I'm actually the Chief Technical Officer of Better Days web agency.

Recommended

Send push notification to iPhone with PHP and pushme.to

UPDATE: 2013/11/18 This code no longer works, but you can check a similar function here: Sending push notifications with php…

August 9, 2010

Add Internet Explorer class to body to detect old browser

When you have clients that still uses an old browser, you need to handle it. This javascript function detects the…

February 27, 2017

Modify list counter in :before pseudo class with jQuery using start attribute

Suppose you have two ordered list ol tags, and the second one has a start attribute. You also have in…

April 20, 2016

Push notifications from php to Android devices with Minibots Class

More than two years ago I wrote a post about sending push notifications from php to an iOS app called…

November 18, 2013

Refresh a Google Adsense banner with Javascript

Often bloggers and site owners use galleries to have a greater number of pages, adding pages means adding clicks. Each…

November 9, 2013

User agent detect with PHP

Small PHP code snippet to make a quick detect if the browser that requests your page is on a mobile…

November 7, 2013