Detecting whether Android device is a phone or a tablet with javascript

一曲冷凌霜 提交于 2019-12-17 19:34:21

问题


I have just built a mobile site and want to redirect the traffic from our main site if the user is browsing through a mobile phone.

Currently I am using javascript to pick up the browser useragent and using that to identify if the device is a mobile phone.

The issue I have is with Android devices as I cannot find a distinguishing feature for Android useragents to decide whether the device is a phone or a tablet, If the user is on a tablet I still want to show them the full site.

Does anyone know of a solution for this?


回答1:


You should probably use this sugestion made by google by reading the user agent. If the UA has the word mobile it's a phone, if it doesn't it's a tablet

http://googlewebmastercentral.blogspot.com/2011/03/mo-better-to-also-detect-mobile-user.html




回答2:


There is no general way of doing this: read this article for more explanation http://www.webvanta.com/post/2012-04-08/how-to-reliably-tell-android-tablets-from-phones

quoting the article in case you dont have the time "Depending on the phone manufacturer and the browser in use, there are phones that don’t include “mobile” in the UA string, and tablets that do"

That is true at lease for tablets, IPads have the word mobile in user agent (for some version of chrome and safari)



来源:https://stackoverflow.com/questions/7173642/detecting-whether-android-device-is-a-phone-or-a-tablet-with-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!