Saturday, December 29, 2012

How to detect iPhone, iPod or iPad using javascript

Most of the sites have a mobile version now a days. In fact they have multiple mobile versions of their site as screen size varies between phone and tablet. To have a mobile version for your site, you need to detect the client first. Detecting client can be done either on the server side (using your preferred scripting language) or the client side. I am showing here how you can detect it on the client side using Javascript. After detecting the client type, you can render an appropriate version of the site. Even you can use this technique to make your jQuery plugin mobile compatible.
var client;

if( navigator.userAgent.match(/iPhone/i) )
{
   client = 'iPhone';   
}
else if( navigator.userAgent.match(/iPod/i) )
{
   client = 'iPod';   
}
else if( navigator.userAgent.match(/iPad/i) ) 
{
   client = 'iPad';   
}

No comments:

Post a Comment