This code snippet used to work. I think a recent update of Google Chrome broke it. Does anyone else see this?
function DenyIfTablet() {
try {
document.createEvent("TouchEvent");
alert("Silverlight not supported on touch-screen devices.");
window.location = document.referrer;
}
catch(exception){
//OK to continue.
}
}
Under IE9 an exception is thrown and the alert/redirect does not occur.
Under Chrome 17.0.963.43 an exception is not thrown and I am redirected away, even though I am not using a touch screen device.
Am I doing something improper here, or did the rug just get pulled out beneath me in a Chrome update?
Yes, there was some change in Chrome 17 and 18 that made that test not work, but it works again in 19. However, that test probably isn’t robust enough in the face of the many different ways browsers choose to express their support for touch.
The modernizr guys did a bunch of research on this, with the results over many hundreds of versions of browsers, mobile and desktop, here:
http://modernizr.github.com/Modernizr/touch.html
My understanding is that for better or worse (mostly worse), there isn’t a silver bullet for detecting support (or even an agreed upon standard way of doing so someday).
The primary way modernizr detects support is
but then they proceed to inject an element with a ‘touch-enabled’ media query and test if it has an effect to be extra sure (it’s a little hard to follow, but check out the source).
So, I would at least consider switching to testing for ontouchstart (which looks closest to right in that browserscope list), but, if you really want to get it right, I would just use modernizr.