触摸检测Blackberry 9300 6.0

Joh*_*hnC 7 html javascript mobile jquery blackberry

我目前在使用的插件中遇到一些触摸检测问题.

该插件使用以下代码

touch = ("ontouchstart" in window) || window.DocumentTouch && document instanceof DocumentTouch;
eventType = (touch) ? "touchend" : "click";
Run Code Online (Sandbox Code Playgroud)

确定是否应在某些图库导航中使用touchend或click事件.

然而,遗憾的是,当使用运行OS 6.0的Blackberry 9300访问该页面时,其错误地报告为启用触摸的设备并且事件不会触发.

我检查了使用的检测方法,它与Modernizr中的检测方法相同.

有没有人有这个问题的解决方案?

Ant*_*los 6

根据Paul Irish的说法,RIM对此问题说:

不幸的是,我们在BlackBerry 6.0期间遇到了构建系统问题,导致构建启用了WebKit触摸支持,即使对于非触摸设备也是如此.它早已修复,但一些公共构建确实存在这个问题.

查看github/Modernizr上的这些(当前打开的)门票,了解可能的解决方法和最新的检测代码,然后根据需要尝试更改插件.如果下面的最新检测代码不起作用,您可能需要专门检测黑莓.

同时检查此触摸测试,browsercope选项卡指示黑莓9000已被检测为false,因此值得一试在您的设备中进行测试.http://modernizr.github.com/Modernizr/touch.html

最新的Modernizr的触摸检测源似乎有除了你的代码发布了另外一个@media检测.

/*
 * The Modernizr.touch test only indicates if the browser supports
 *    touch events, which does not necessarily reflect a touchscreen
 *    device, as evidenced by tablets running Windows 7 or, alas,
 *    the Palm Pre / WebOS (touch) phones.
 *
 * Additionally, Chrome (desktop) used to lie about its support on this,
 *    but that has since been rectified: crbug.com/36415
 *
 * We also test for Firefox 4 Multitouch Support.
 *
 * For more info, see: modernizr.github.com/Modernizr/touch.html
 */

tests['touch'] = function() {
    var bool;

    if(('ontouchstart' in window) || window.DocumentTouch && document instanceof DocumentTouch) {
      bool = true;
    } else {
      injectElementWithStyles(['@media (',prefixes.join('touch-enabled),('),mod,')','{#modernizr{top:9px;position:absolute}}'].join(''), function( node ) {
        bool = node.offsetTop === 9;
      });
    }

    return bool;
};
Run Code Online (Sandbox Code Playgroud)

BlackBerry/PlayBook UA嗅探

要使用用户代理字符串专门检测BlackBerry设备并借用此处此处提供的解决方案,我启动了这个小功能,您可以在http://jsbin.com/aliwur/1/edit#上测试并查看jsbin的工作情况javascript,live,它应该从用户代理字符串解析Blackberry 5.0/4.0/6.0和Playbook:

function rim_useragent_parser(ua) {

    var info = false,
        model = null,
        model_number = null,
        os_version = null;

    if (ua.indexOf("BlackBerry") >= 0) {
        if (ua.indexOf("Version/") >= 0) {
            // BlackBerry 6 and 7
            model = ua.match(/BlackBerry\s[0-9]*/);
            if (model) {
                model_number = model[0].match(/[0-9]+/);
                pos = ua.indexOf("Version/") + 8;
                os_version = ua.substring(pos, pos + 3);
                info = {
                    'model' : model[0],
                    'model_number' : model_number[0],
                    'os_version' : os_version
                };
            }
        }
        else {
            // BlackBerry Device Software 4.2 to 5.0
            model = ua.match(/^BlackBerry[0-9]*/);
            if (model) {
                model_number = model[0].match(/[0-9]+/);
                var SplitUA = ua.split("/");
                os_version = SplitUA[1].substring(0, 3);
                info = {
                    'model' : model[0],
                    'model_number' : model_number[0],
                    'os_version' : os_version
                };
            }
        }
    }
    else if (ua.indexOf("PlayBook") >= 0) {
        // PlayBook
        model = ua.match(/RIM Tablet OS\s[0-9].[0-9].[0-9]/);
        if (model) {
            model_number = model[0].match(/[0-9].[0-9].[0-9]/);
            pos = ua.indexOf("Version/") + 8;
            os_version = ua.substring(pos, pos + 5);
            info = {
                'model' : model[0],
                'model_number' : model_number[0],
                'os_version' : os_version
            };
        }
    }

    return info;

}
Run Code Online (Sandbox Code Playgroud)

当然,你可能需要将它简化为"Blackberry 9300 6.0",我想你也可以这样做:

var ua = navigator.userAgent;
if (ua.indexOf("BlackBerry") >= 0) {
    if (ua.indexOf("Version/") >= 0) {
        // BlackBerry 6 and 7
        var model = ua.match(/BlackBerry\s[0-9]*/);
        if (model) {
            var model_number = model[0].match(/[0-9]+/);
            if (model_number) model_number = model_number[0];
            pos = ua.indexOf("Version/") + 8;
            os_version = ua.substring(pos, pos + 3);

            if (os_version === '6.0' && model_number === '9300') {
                // do what you need specifically for this
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

为了更好的通用用户代理解析,请参阅ua-parser

https://github.com/tobie/ua-parser/