iOS Safari独立的in-webapp导航突然无法正常工作

时间:2018-05-03 13:28:44

标签: javascript ios mobile-safari iphone-standalone-web-app

在iOS中,它的独立网络应用程序模式可以阻止在Mobile Safari中打开链接,而不是在webapp本身中打开:

(function(document,navigator,standalone) {
// prevents links from apps from oppening in mobile safari
// this javascript must be the first script in your <head>
if ((standalone in navigator) && navigator[standalone]) {
    var curnode, location=document.location, stop=/^(a|html)$/i;
    document.addEventListener('click', function(e) {
        curnode=e.target;
        while (!(stop).test(curnode.nodeName)) {
            curnode=curnode.parentNode;
        }
        // Condidions to do this only on links to your own app
        // if you want all links, use if('href' in curnode) instead.
        if(
            'href' in curnode && // is a link
            (chref=curnode.href).replace(location.href,'').indexOf('#') && // is not an anchor
            (   !(/^[a-z\+\.\-]+:/i).test(chref) ||                       // either does not have a proper scheme (relative links)
                chref.indexOf(location.protocol+'//'+location.host)===0 ) // or is in the same protocol and domain
        ) {
            e.preventDefault();
            location.href = curnode.href;
        }
    },false);
}
})(document,window.navigator,'standalone');

这已经有一段时间了,但由于Apple的新iOS更新每个链接都在Mobile Safari中开放。

调试后我发现代码中的条件仍然正确,因为在独立模式下location.href = curnode.href;仍然被命中/执行。

所以我认为:更改文档它的href会加载已被点击的链接的页面不再起作用(更严格的规则左右?)

我尝试通过执行以下操作来更改路径名而不是完整网址:
location.pathname = curnode.getAttribute('path');
(想一想如果只改变了路径,那么webapp将保留在webapp中)

那里是否还有人面临这个问题,或者有人可以帮我思考解决方案的正确方向,或者更好但不那么有趣,有解决方案?

1 个答案:

答案 0 :(得分:0)

找到解决方案。我现在使用location.href = curnode.href;

而不是location.assign(curnode.href);
相关问题