How To Detect Mobile Devices With JavaScript


Sometimes we may need to know whether or not we’re dealing with a mobile device. Using JavaScript we can get it by parsing the UserAgent string.

For example if you are using Apple iPad the userAgent will return the value –
Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.10.

Lets see how we can get the userAgent and use it to determine the device.

var uagent = navigator.userAgent.toLowerCase(); 
var device = '';
if ("iphone") > -1)
       device = 'iphone';
else if ("ipod") > -1)
       device = 'ipod';
else if ("tablet") > -1)
       device = 'tablet';
else if ("ipad") > -1)
       device = 'ipad';
else if ("android") > -1)
       device = 'android';
This entry was posted in Browser, Javascript and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *