Tuesday, February 10, 2009

How to identify if the device is iPhone or iPod Touch using JavaScript browser detection script

If you are writing an iPhone/iPod Touch specific web application or converting your web site to be compatible with iPhone, or maybe using iPhone mobile Safari specific features in all of these cases you need to detect the device that your application is running on and the browser. So this is a little JavaScript code snippet that will detect if the current browser is an iPhone or iPod Touch.

This script is also featured on jQuery iPhone detact post.

function isiPhone(){
   
return (
       
(navigator.platform.indexOf("iPhone") != -1) ||
       
(navigator.platform.indexOf("iPod") != -1)
   
);
}

/* User Agent String for iPhone

    Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko)
    Version/3.0 Mobile/1A543a Safari/419.3
    
    User Agent String for iPod Touch

    Mozilla/5.0 (iPod; U; CPU like Mac OS X; en) AppleWebKit/420.1 (KHTML, like Gecko)
    Version/3.0 Mobile/3A101a Safari/419.3
*/

No comments:

Post a Comment