Javascript – Responsive Web Design vs. User-Agent Sniffing

cssjavascriptweb-development

The CSS3 media query feature has led to many interesting possibilities in terms of developing websites which adjust to many different screen sizes and devices.

However, in practice, I'm starting to get the sense that the CSS3 media query feature, and the whole "Responsive Web Design" movement, may not live up to its promise.

The problem I see is that, at the end of the day, web developers care mostly about whether their users are viewing content via a Desktop, Tablet, or Mobile device. But CSS3 only provides a means to detect screen resolution. In theory, detecting screen resolution seems like a great way to adjust for various different devices. But in practice…

Suppose we have a simple Javascript function that just outputs the screen width:

function foo()
{
   alert(screen.width);
}

On my Blackberry Touch, this outputs:

768

On my Samsung Galaxy, this outputs:

800

So…um, at this point, the resolution of mainstream smart phones is getting pretty close to Desktop-level resolutions. The ability to detect whether a user is viewing your website via a smartphone, tablet, or desktop, seems to be increasingly difficult if all you're going by is screen resolution.

This makes me call into question the entire wisdom behind the whole CSS3 "Responsive Web Design" movement based on media queries. It almost seems like the media query feature is better suited towards adapting to a resizing browser window on a Desktop screen, rather than various mobile devices.

Another possible technique for detecting mobile or tablet devices is to use feature detection, by checking if the ontouchstart event is supported. But even this is becoming increasing unreliable as many Desktop screens start to support touch.

Question: So… as a web developer, if I can't rely on RWD or feature detection, is user-agent sniffing (as notoriously unreliable as always) really the best option to detect mobile devices?

Best Answer

Stop worrying so much about specific devices.

The ability to detect whether a user is viewing your website via a smartphone, tablet, or desktop, seems to be increasingly difficult if all you're going by is screen resolution

It is getting increasingly difficult to detect via screen resolution, and it will only get harder as more devices enter the market, but the purpose of media queries is not to detect device types.What media queries are good at is making tweaks to your design when it is no longer pleasant to use at the current dimensions. If the site starts to fall apart at 550px wide, then it doesn't matter if there's a device with those dimensions or not: you set the breakpoint there.

Its the same deal with feature detection. If the device supports touch, then what does it matter if it's a mobile device or some future wall-sized monitor? Either way the touch enhancements will probably be useful.

User-agent sniffing - as you've stated - is completely unreliable. I could change my User-agent string to Shakespeare quotes if I really wanted too. What would your site decide about my browser then?

User agents also require a lot of work to handle all the possibilities. Do you have one for Firefox on android? Chrome on IOS? Dolphin on Froyo? The WiiU Browser? The extremely limited browser on my old LG feature phone? Lynx? IE 13? Links? IceWeasel? The Blackberry playbook's browser? How are you telling the difference between Opera running on a tablet and opera running on a phone?

This list can only grow as time goes on.

Related Topic