独立的Web应用程序链接脚本 - 阻止它附加具有特定类的链接?

时间:2013-04-20 02:55:26

标签: javascript jquery iphone web-applications

此脚本可防止在Mobile Safari中打开链接。这适用于iOS应用程序模式(主屏幕书签)上的Web应用程序。

gist.github.com/1042026

if(("standalone" in window.navigator) && window.navigator.standalone){

    // If you want to prevent remote links in standalone web apps opening Mobile Safari, change 'remotes' to true
    var noddy, remotes = false;

    document.addEventListener('click', function(event) {

        noddy = event.target;

        // Bubble up until we hit link or top HTML element. Warning: BODY element is not compulsory so better to stop on HTML
        while(noddy.nodeName !== "A" && noddy.nodeName !== "HTML") {
            noddy = noddy.parentNode;
        }

        if('href' in noddy && noddy.href.indexOf('http') !== -1 && (noddy.href.indexOf(document.location.host) !== -1 || remotes))
        {
            event.preventDefault();
            document.location.href = noddy.href;
        }

    },false);
}

我的问题是如何让上面的脚本忽略与类的链接?

例如,我a.lightbox - 此链接打开并使用www.photoswipe.com jquery插件在灯箱中进行图像处理。

但是当我使用脚本时,它会杀死灯箱启动,它只是自己打开图像。但如果我上面的这个脚本,photoswipe灯箱工作正常。

有没有人得到一些建议或帮助修改此脚本以忽略这些链接a.lightbox

由于 约什

1 个答案:

答案 0 :(得分:1)

如果您正在使用jQuery,那么您应该只需将&& !$(noddy).hasClass('lightbox')添加到if测试中。