Вопрос

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)/);
Это было полезно?

Решение

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

Другие советы

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

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top