如何使用javascript检测平板电脑,手机,桌面,电视 [英] How to detect tablet, mobile, desktop, tv using javascript

查看:122
本文介绍了如何使用javascript检测平板电脑,手机,桌面,电视的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Web应用程序,我需要从javascript中检测设备。谁有人告诉我最好的方法呢?我通过谷歌搜索找到了几个解决方案。但没有人能够充分证明。我不想像WURFL一样使用服务器端检测。



我的更新



我通过匹配检测设备使用javascript的CSS媒体查询。使用保罗爱尔兰的polyfill进行媒体查询匹配
https://github.com/paulirish/matchMedia.js/

解决方案

我建议你查看 http:/ /wurfl.io/



简而言之,如果您导入一个微小的JS文件:

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

您将留下一个类似于以下内容的JSON对象:

  {
complete_device_name:Google Nexus 7,
is_mobile:true,
form_factor:平板电脑
}

(假设您使用的是Nexus 7)将能够执行以下操作:

  if(WURFL.form_factor ==Tablet){
//做东西();
}

这就是你要找的。

免责声明:我为提供此免费服务的公司工作。谢谢。


I am developing an web application where I need to detect the device from javascript. Could anybody tell me whats the best way to do it ? I found several solution by googling. But no one is full proof. I do not want to use server side detection like WURFL.

My update

I am detecing device by matching CSS media queries using javascript. For media query matching using polyfill by Paul Irish https://github.com/paulirish/matchMedia.js/

解决方案

I advise you check out http://wurfl.io/

In a nutshell, if you import a tiny JS file:

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

you will be left with a JSON object that looks like:

{
 "complete_device_name":"Google Nexus 7",
 "is_mobile":true,
 "form_factor":"Tablet"
}

(that's assuming you are using a Nexus 7, of course) and you will be able to do things like:

if(WURFL.form_factor == "Tablet"){
    //dostuff();
}

This is what you are looking for.

Disclaimer: I work for the company that offers this free service. Thanks.

这篇关于如何使用javascript检测平板电脑,手机,桌面,电视的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆