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

And*_*ell 5 html javascript mobile ipad ios8

15 年 14 月 7 日更新 失去所有希望......我假设我的修复将与 javascript 相关,但我根本不知道。我当前的代码是否阻止了我的 HTML target="Blanks" 工作?

我正在制作一个在 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;
    }
    });
    }
    });
Run Code Online (Sandbox Code Playgroud)

这很好,很棒。我希望网站在自己的窗口中工作,看起来像一个本机应用程序。

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

target="_blank" 不起作用,或者 rel="external"

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

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

And*_*ell 5

我找到了答案!从这里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>
Run Code Online (Sandbox Code Playgroud)

和链接的标准 HTML

<a href="http://extneral" target="_blank">your link</a>
Run Code Online (Sandbox Code Playgroud)

这样 web 应用程序下的本地链接仍然在同一个窗口中打开,但外部 http:// 链接将在 Safari 中打开