在iOS 8中打开从Web应用程序到新Safari窗口的链接

时间:2015-07-08 17:18:06

标签: javascript html ipad mobile ios8

更新07.14.15 失去所有希望...... 我假设我的修复程序将与javascript相关,但我根本不知道。我当前的代码是否停止了我的HTML目标=“空白”?

我正在制作一个在iPad Mini上下载的HTML / CSS应用程序。这是一个简单的30多页网站,内有文字和图片。我使用这个javascript来使我的所有href在应用程序中打开:

$(document).ready(function(){
if (("standalone" in window.navigator) && window.navigator.standalone) {
  // For iOS Apps
  $('a').on('click', function(e){
    e.preventDefault();
    var new_location = $(this).attr('href');
    if (new_location != undefined && new_location.substr(0, 1) != '#' && $(this).attr('data-method') == undefined){
      window.location = new_location;
    }
    });
    }
    });

哪个好,很好。我希望该网站在自己的窗口中工作,看起来像一个原生应用程序。

现在我需要在Safari中打开这个ONE链接(它必须打开Safari并切换到该窗口)。

target =“_ blank”不起作用,或者rel =“external”

如何在Web应用程序中打开链接以及在Safari中打开其他链接?

这个问题How to open Safari from a WebApp in iOS 7与我的相似,但修复程序对我不起作用,我现在在iOS 8中。

1 个答案:

答案 0 :(得分:4)

我找到了答案!从这里https://gist.github.com/kylebarrow/1042026

头部

中的这个脚本
<script>
// Mobile Safari in standalone mode
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);
}
</script>

和链接的标准HTML

<a href="http://extneral" target="_blank">your link</a>

这样,网络应用下的本地链接仍会在同一窗口中打开,但外部http://链接将在Safari中打开