PC 上的 Google Chrome 在 document.createEvent(“TouchEvent”) 上取得了成功...?
该代码片段曾经有效。我认为 Google Chrome 最近的更新破坏了它。还有其他人看到这个吗?
function DenyIfTablet() {
try {
document.createEvent("TouchEvent");
alert("Silverlight not supported on touch-screen devices.");
window.location = document.referrer;
}
catch(exception){
//OK to continue.
}
}
在 IE9 下,会引发异常并且不会发生警报/重定向。
在 Chrome 17.0.963.43 下,不会抛出异常,并且我被重定向离开,即使我没有使用触摸屏设备。
我在这里做了什么不正当的事情吗,还是在 Chrome 更新中地毯被拉出了我下面?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,Chrome 17 和 18 中发生了一些变化,导致该测试不起作用,但在 19 中它再次起作用。但是,面对浏览器选择表达支持的多种不同方式,该测试可能不够稳健。触碰。
modernizr 人员对此进行了大量研究,其结果遍及数百个版本的浏览器、移动设备和桌面,此处:
http://modernizr.github.com/Modernizr/touch.html
我的理解是无论好坏(大多数情况下更糟),都没有检测支持的灵丹妙药(甚至没有一个商定的标准方法来检测支持)。
Modernizr 检测支持的主要方式是
,然后他们继续注入一个带有“支持触摸”媒体查询的元素,并测试它是否有效果以确保更加确定(这有点难以理解,但请查看 来源)。
所以,我至少会考虑切换到 ontouchstart 测试(它在 browserscope 列表中看起来最接近右侧),但是,如果你真的想得到它的正确,我只会使用modernizr。
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.