Determining if the Page Is Being Viewed on a Mobile Device

if( /Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(   navigator.userAgent) ) {    . . . mobile code here . . .  }

You can use the following JavaScript and regex statement to parse the navigator.userAgent value and determine whether a user is coming into your web site on a mobile device:

if( /Android|webOS|iPhone|iPad|iPod|BlackBerry/ i.test(navigator.userAgent) ) { }

The statement parses the userAgent attribute and tries to find the most common strings incorporated in the mobile browsers.

Get jQuery and JavaScript Phrasebook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.