【问题标题】:Open a link from web app to new Safari window in iOS 8在 iOS 8 中打开从 Web 应用程序到新 Safari 窗口的链接
【发布时间】:2015-09-26 18:15:17
【问题描述】:

更新 07.14.15 失去所有希望... 我假设我的修复将与 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;
    }
    });
    }
    });

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

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

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

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

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

【问题讨论】:

    标签: javascript html ipad mobile ios8


    【解决方案1】:

    我找到了答案!从这里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 中打开

    【讨论】:

    • 希望这对使用 rails 的人有用。只需添加remote: true,如下所示:link_to "hello", hello_path, remote: true。它适用于 iOS 9.2 和 rails 4.2
    猜你喜欢
    • 2020-04-22
    • 2011-02-23
    • 2020-04-12
    • 1970-01-01
    • 2012-02-11
    • 1970-01-01
    • 1970-01-01
    • 2016-01-05
    • 2014-04-23
    相关资源
    最近更新 更多