3
votes

I am trying to detect "desktop", "mobile", and "tablet" from user agent strings. What I need is just these three rough categories. Brand and device names are not needed. Some examples of user agent I have are:

"Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; InfoPath.3)"

"Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.104 Safari/537.36"

Is there any existing API can do this?

According to the very last sentence of this article, it seems that looking for keyword "mobi" in a user agent string can handle most cases. But it seems too easy to be true......

2

2 Answers

0
votes

You might use UADetector library that does just that. It has device categorization feature added since 0.9.10 version.

UADetector is a library to identify over 190 different desktop and mobile browsers and 130 other User-Agents like feed readers, email clients and multimedia players. In addition, even more than 400 robots like BingBot, Googlebot or Yahoo Bot can be identified.

0
votes

I'm curious: Why does the device type matter?

Not trying to be snarky, but trying to understand a use case.

In Cordova you can check for this, but now sure about straight HTML. I which I have done for iOS before. I needed to do this to detect when I needed to add a 20px margin to the header of a page on a Cordova app, as iOS made this change several versions ago.

Here is how I did it:

    if (parseFloat(window.device.version) >= 7.0 ) {
        $allHeaders.css( "margin-top", "20px" );       
    }