javascript – 触摸检测Blackberry 9300 6.0

前端之家收集整理的这篇文章主要介绍了javascript – 触摸检测Blackberry 9300 6.0前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我目前在使用的插件中遇到一些触摸检测问题.

插件使用以下代码

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

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

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

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

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

解决方法

根据 Paul Irish,RIM said这个问题:

Unfortunately,we had a build system issue during BlackBerry 6.0 that caused builds to have WebKit touch support enabled,even for non-touch devices. It’s long been fixed,but some public builds do have this issue.

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

> https://github.com/Modernizr/Modernizr/issues/372
> https://github.com/Modernizr/Modernizr/issues/548

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

除了您发布的代码之外,latest 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;
};

BlackBerry / PlayBook UA嗅探

要使用用户代理字符串专门检测BlackBerry设备并借鉴herehere给出的解决方案,我启动了这个小功能,您可以在http://jsbin.com/aliwur/1/edit#javascript,live测试并查看jsbin的工作,它应该解析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],'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],'os_version' : os_version
            };
        }
    }

    return info;

}

当然,你可能需要将它简化为“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
            }
        }
    }
}

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

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

原文链接:https://www.f2er.com/js/157484.html

猜你在找的JavaScript相关文章