如何使用JavaScript检测设备类型 [英] How to detect device type using javascript

查看:53
本文介绍了如何使用JavaScript检测设备类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我的更新

我正在通过使用JavaScript匹配CSS媒体查询来检测设备.对于使用Paul Irish的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":"Tablet"
}

(当然,这是假设您使用的是Nexus 7),您将可以执行以下操作:

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

这就是您要寻找的.

免责声明:我为提供这项免费服务的公司工作.谢谢.

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天全站免登陆