简体   繁体   中英

Auto detect only mobile browser (via user-agent?) not tablet

I want to apply meta tag only for mobile devices not for tablet. The purpose is to zoom out the page in mobie devices. I got below condition but the problem is it is also applying on tablet as there is also android tablets.

<script>
    if(navigator.userAgent.match(/Android/i)
  || navigator.userAgent.match(/webOS/i)
  || navigator.userAgent.match(/iPhone/i)
  || navigator.userAgent.match(/iPod/i)
  || navigator.userAgent.match(/BlackBerry/i)
  || navigator.userAgent.match(/Windows Phone/i)) {
    document.write('<meta name="viewport" content="width=device-width, user-scalable=yes, initial-scale=0.25" />');
}
    </script>

I know this is an old question but i am not able to find the relevent answer.

Mobile Android has "Mobile" string in the User-Agent header. Tablet Android does not. So to get mobile only, you need to search for the word "Mobile" as well as "Android". However there are some tablet companies that are going away from this convention, so for those, like the XOOM, you will need to check for model of the tablet if you want to detect those tablets.

You can also check out this: http://wurfl.io/ . I think this could help by doing something like the following:

<script type='text/javascript' src="//wurfl.io/wurfl.js"></script>

if(WURFL.form_factor != "Tablet"){
     // do mobile things here
}

For context, the script returns a json object like so:

{
 "complete_device_name":"Samsung Galaxy Tab",
 "is_mobile":true,
 "form_factor":"Tablet"
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM