What's the best way to detect a webOS tablet with jQuery / plain JS

对着背影说爱祢 提交于 2019-12-11 04:38:01

问题


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

So an easy way would be:

var deviceAgent = navigator.userAgent.toLowerCase();
webOS = deviceAgent.match(/(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)/);

回答1:


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.]+)/)

(The 2nd capture is the version)

From detect.js




回答2:


Here's a function in PHP that will detect WebOS and any other mobile device you could need. Less than 1kb in code =)

function detectMobileDevice() {
    if(preg_match('/(android|avantgo|blackberry|bolt|boost|cricket|docomo|fone|hiptop|mini|mobi|palm|phone|pie|tablet|up\.browser|up\.link|webos|wos)/i', $_SERVER['HTTP_USER_AGENT'])) {
        return true;
    }
    else {
        return false;
    }
}

if you want to do ONLY webOS, change line 2 to:

if(preg_match('/(webos)/i', $_SERVER['HTTP_USER_AGENT'])) {

to use:

if(detectMobileDevice()) {
    // If mobile device detected, do something
}
else {
   // Otherwise, do something else...
}

if you need more details, visit here: http://www.justindocanto.com/scripts/detect-a-mobile-device-in-php-using-detectmobiledevice



来源:https://stackoverflow.com/questions/7173573/whats-the-best-way-to-detect-a-webos-tablet-with-jquery-plain-js

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