O'Reilly logo

jQuery and JavaScript Phrasebook by Brad Dayley

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required