How To Detect Mobile Devices With JavaScript

Share

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 (uagent.search("iphone") > -1)
       device = 'iphone';
else if (uagent.search("ipod") > -1)
       device = 'ipod';
else if (uagent.search("tablet") > -1)
       device = 'tablet';
else if (uagent.search("ipad") > -1)
       device = 'ipad';
else if (uagent.search("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 *

*