Hacker News new | ask | show | jobs
by iron_ball 4331 days ago
"In general, our advice is to develop a responsive site that can adapt to the capabilities of different devices. If you choose to build a mobile-specific experience then we recommend looking for the sub-string "mobile" in the user agent string to determine when to deliver mobile optimised content:"

    function isMobile() {
        return navigator.userAgent.toLowerCase().indexOf("mobile")>=0;
    }
This is really the best we can do after twenty years of the WWW?
4 comments

Browser identification is an arms race. It harkens back to the original user agent wars near the turn of the millenium. I think it's unlikely to ever have a solid way to identify browsers. If one is developed, it will be used by (bad?) developers as an exclusion check for their fancy content. That gives other browsers an incentive to lie. I think the whole concept is inherently unstable for this reason.
No. Feature detection (which is also mentioned in the article) is far superior
or do user-agent detection server-side against database of (mostly) known user-agents and take JS out of the UA detection picture entirely.

In JVM land I've had pretty good success with UADetector [1] for delivering device specific content to mobile phones and desktop/laptop/tablets.

[1] https://github.com/before/uadetector

ug, that's already a bad check, but if they insist, why not just:

    /mobile/i.test(navigator.userAgent)
> Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.

Just because the API call and syntax is a bit iffy doesn't mean it's a bad pattern. ES6 will finally have a string .contains method btw (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...)

I'd add \b around to avoid matching e.g. `Immobile/1.0`, the slow browser.