PC上的谷歌浏览器在document.createEvent("TouchEvent")上取得了成功......?

Sea*_*son 1 silverlight jquery google-chrome tablet

此代码段曾用于工作.我认为谷歌Chrome最近的更新打破了它.有没有人看到这个?

function DenyIfTablet() {
    try {
        document.createEvent("TouchEvent");
        alert("Silverlight not supported on touch-screen devices.");
        window.location = document.referrer;
    }
    catch(exception){ 
        //OK to continue.
    }
}
Run Code Online (Sandbox Code Playgroud)

在IE9下,抛出异常并且不会发生警报/重定向.

在Chrome 17.0.963.43下,即使我没有使用触摸屏设备,也不会抛出异常并重定向.

我在这里做了一些不正确的事情,还是在Chrome更新中将地毯拉到了我的下方?

Bre*_*nny 5

是的,Chrome 17和18中有一些变化使得该测试不起作用,但它在19中再次起作用.但是,面对浏览器选择表达其支持的许多不同方式,该测试可能不够强大触摸.

Modernizr的家伙做了一堆关于这项研究,有结果了数百浏览器,移动和桌面,这里的版本:

http://modernizr.github.com/Modernizr/touch.html

我的理解是,无论好坏(大多数情况下更糟),都没有用于检测支持的灵丹妙药(或者甚至是某一天商定的标准方式).

modernizr检测支持的主要方式是

('ontouchstart' in window) || window.DocumentTouch && document instanceof DocumentTouch
Run Code Online (Sandbox Code Playgroud)

但随后他们继续注入一个带有"启用触摸"的媒体查询的元素,并测试它是否具有更加确定的效果(这有点难以理解,但请查看源代码).

所以,我至少会考虑切换到ontouchstart的测试(在browsercope列表中看起来最接近正确),但是,如果你真的想要正确,我会使用modernizr.