问题描述
我正在寻找使用纯JS检测webOS平板电脑的最佳方法,如果有的话,也可以使用jQuery.平板电脑的用户代理应如下所示:
I'm looking for the best way to detect a webOS tablet using plain JS and if it's any easier also using jQuery. The user agent of the tablet should look something like this:
User-Agent:Mozilla/5.0 (webOS/1.3; U; en-US) AppleWebKit/532.2 (KHTML, like Gecko) Version/1.0 Safari/532.2 Desktop/1.0
一个简单的方法是:
var deviceAgent = navigator.userAgent.toLowerCase();
webOS = deviceAgent.match(/(webos)/);
这已经是最好的方法了吗?您可能会说检测需要确定存在的功能,但这对我不起作用,因为我想要的功能已存在,但无法像在任何台式机上一样正常工作,所以我真的只想知道不论是否是webOS设备.
Is that the best way to do it already? You're likely going to say detect the feature you need to make certain is present but that won't work for me because the feature I want is present but not working as it would on any desktop, so I really just want to know is this a webOS device or not.
更新:刚刚发现平板电脑确实使用了另一个用户代理:
Update: Just found that the tablet really uses another user agent:
Mozilla/5.0 (hp-tablet; Linux; hpwOS/3.0.0; U; xx-xx) AppleWebKit/534.6 (KHTML, like Gecko) wOSBrowser/233.48 Safari/534.6 TouchPad/1.0
因此,上述内容可能应该是:
So the above should probably rather be:
var deviceAgent = navigator.userAgent.toLowerCase();
webOS = deviceAgent.match(/(webos|hpwos)/);
推荐答案
我不知道您是否可以进行只能识别WebOS的功能检测.它是基于WebKit的,因此所有其他基于WebKit的平台都将具有相同的功能.查看 Zepto.js'源代码,它们的行为与您完全相同:
I don't know if you can do any feature detection that'll only identify WebOS. It's WebKit-based, so all other WebKit-based platforms will have the same features. Looking at Zepto.js' source, they're doing exactly the same as you are:
ua.match(/(webOS|hpwOS)[\s\/]([\d.]+)/)
(第二次捕获是版本)
来自 detect.js
这篇关于用jQuery/Plain JS检测WebOS平板电脑的最佳方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!