iPhone Safari Web App在新窗口中打开链接

时间:2010-05-24 17:05:15

标签: iphone safari iphone-standalone-web-app

将图标添加到主屏幕后,我遇到了网络问题。如果从主屏幕启动Web,则所有链接将在Safari的新窗口中打开(并失去全屏功能)。我该怎样预防呢?我找不到任何帮助,只有同样没有答案的问题。

20 个答案:

答案 0 :(得分:106)

我在iWebKit框架中找到了JavaScript解决方案:

var a=document.getElementsByTagName("a");
for(var i=0;i<a.length;i++)
{
    a[i].onclick=function()
    {
        window.location=this.getAttribute("href");
        return false
    }
}

答案 1 :(得分:90)

此处的其他解决方案要么不考虑外部链接(您可能希望在Safari外部打开),要么不考虑相关链接(不包含域名)。

html5 mobile-boilerplate项目链接到这个主题,该主题对该主题进行了很好的讨论:https://gist.github.com/1042026

以下是他们提出的最终代码:

<script>(function(a,b,c){if(c in b&&b[c]){var d,e=a.location,f=/^(a|html)$/i;a.addEventListener("click",function(a){d=a.target;while(!f.test(d.nodeName))d=d.parentNode;"href"in d&&(d.href.indexOf("http")||~d.href.indexOf(e.host))&&(a.preventDefault(),e.href=d.href)},!1)}})(document,window.navigator,"standalone")</script>

答案 2 :(得分:46)

如果您使用的是jQuery,则可以执行以下操作:

$("a").click(function (event) {
    event.preventDefault();
    window.location = $(this).attr("href");
});

答案 3 :(得分:20)

这适用于iOS 6.1和Bootstrap JS链接(即下拉菜单等)

$(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;
        }
      });
    }
  });

答案 4 :(得分:8)

这是一个老问题,这里的许多解决方案都使用javascript。从那时起,iOS 11.3已经发布,您现在可以使用scope member。范围成员是类似"/"的URL,其中该范围下的所有路径都不会打开新页面。

  

scope成员是一个表示导航范围的字符串   这个Web应用程序的应用程序上下文。

以下是我的例子:

{
  "name": "Test",
  "short_name": "Test",
  "lang": "en-US",
  "start_url": "/",
  "scope": "/",
  ...
}

您还可以阅读更多相关信息here。我还建议使用提供此功能的generator

  

如果指定范围,一切都按预期工作,类似于   Android,范围之外的目的地将在Safari中打开 - 用   返回按钮(状态栏中的小按钮)到您的PWA。

答案 5 :(得分:5)

如果使用jQuery Mobile,您将在使用data-ajax ='false'属性时体验新窗口。事实上,只要关闭ajaxEnabled,通过外部链接,通过$ .mobile.ajaxEnabled设置或具有target =''属性,就会发生这种情况。

您可以使用以下方法修复它:

$("a[data-ajax='false']").live("click", function(event){
  if (this.href) {
    event.preventDefault();
    location.href=this.href;
    return false;
  }
});

(感谢Richard Poole的live()方法 - 没有使用bind())

如果您已全局关闭ajaxEnabled,则需要删除[data-ajax ='false']。

这花了我很长时间才弄明白,因为我期待它是一个jQuery Mobile特定的问题,实际上它是实际上禁止新窗口的Ajax链接。

答案 6 :(得分:5)

根据戴维斯的回答和理查兹的评论,你应该进行域名检查。否则,您的网络应用程序中也会打开指向其他网站的链接。

$('a').live('click', function (event)
{      
    var href = $(this).attr("href");

    if (href.indexOf(location.hostname) > -1)
    {
        event.preventDefault();
        window.location = href;
    }
});

答案 7 :(得分:3)

此代码适用于iOS 5(对我有用):

在head标签中:

<script type="text/javascript">
    function OpenLink(theLink){
        window.location.href = theLink.href;
    }
</script>

在您想要在同一窗口中打开的链接中:

<a href="(your website here)" onclick="OpenLink(this); return false"> Link </a>

我从此评论中获得了此代码:iphone web app meta tags

答案 8 :(得分:3)

当目标明确设置为“_blank”时,也许您应该允许在新窗口中打开链接:

$('a').live('click', function (event)
{      
    var href = $(this).attr("href");

    // prevent internal links (href.indexOf...) to open in safari if target
    // is not explicitly set_blank, doesn't break href="#" links
    if (href.indexOf(location.hostname) > -1 && href != "#" && $(this).attr("target") != "_blank")
    {
        event.preventDefault();
        window.location = href;
    }

});

答案 9 :(得分:3)

我发现一个非常完整和高效的,因为它只检查在独立的WebApp下运行,没有jQuery,也很简单,只是在iOS 8.2下测试过:

Stay Standalone: Prevent links in standalone web apps opening Mobile Safari

答案 10 :(得分:2)

你也可以做几乎正常的链接:

<a href="#" onclick="window.location='URL_TO_GO';">TEXT OF THE LINK</a>

你可以删除哈希标签和href,它所做的一切都会影响外观..

答案 11 :(得分:2)

这对iOS 6有用(对rmarscher的答案非常轻微的改编):

<script>                                                                
    (function(document,navigator,standalone) {                          
        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;                         
                }                                                       
                if ("href" in curnode && (curnode.href.indexOf("http") || ~curnode.href.indexOf(location.host)) && curnode.target == false) {
                    e.preventDefault();                                 
                    location.href=curnode.href                          
                }                                                       
            },false);                                                   
        }                                                               
    })(document,window.navigator,"standalone")                          
</script>

答案 12 :(得分:2)

这是Sean的略微改编的版本,它阻止了后退按钮

// this function makes anchor tags work properly on an iphone

$(document).ready(function(){
if (("standalone" in window.navigator) && window.navigator.standalone) {
  // For iOS Apps
  $("a").on("click", function(e){

    var new_location = $(this).attr("href");
    if (new_location != undefined && new_location.substr(0, 1) != "#" && new_location!='' && $(this).attr("data-method") == undefined){
      e.preventDefault();
      window.location = new_location;
    }
  });
}

});

答案 13 :(得分:1)

对于那些使用Twitter Bootstrap和Rails 3的人

$('a').live('click', function (event) {
  if(!($(this).attr('data-method')=='delete')){
    var href = $(this).attr("href");
    event.preventDefault();
    window.location = href; 
  }   
});

删除链接仍然以这种方式工作。

答案 14 :(得分:1)

我更喜欢在独立网络应用模式中打开所有链接,但具有target =“_ blank”的链接除外。当然,使用jQuery。

$(document).on('click', 'a', function(e) {
    if ($(this).attr('target') !== '_blank') {
        e.preventDefault();
        window.location = $(this).attr('href');
    }
});

答案 15 :(得分:1)

我用于iOS网络应用程序的一个解决方法是我将所有链接(通过CSS按钮)形成提交按钮。所以我打开了一个发布到目标链接的表单,然后输入type =“submit” 不是最好的方式,但这是我在找到此页面之前想到的。

答案 16 :(得分:1)

我用@rmarscher's answer创建了一个可以安装的凉亭,可在此处找到:

http://github.com/stylr/iosweblinks

您可以使用bower install --save iosweblinks

轻松安装带有凉亭的代码段

答案 17 :(得分:1)

对于使用JQuery Mobile的用户,上述解决方案会破坏弹出对话框。这将保持webapp中的链接并允许弹出窗口。

$(document).on('click','a', function (event) {
    if($(this).attr('href').indexOf('#') == 0) {
        return true;
    }
    event.preventDefault();
    window.location = $(this).attr('href');     
});

也可以通过以下方式完成:

$(document).on('click','a', function (event){
    if($(this).attr('data-rel') == 'popup'){
        return true;
    }
    event.preventDefault();
    window.location = $(this).attr('href');     
});

答案 18 :(得分:0)

以下是我用于页面上所有链接的内容......

document.body.addEventListener(function(event) {
    if (event.target.href && event.target.target != "_blank") {
        event.preventDefault();
        window.location = this.href;                
    }
});

如果你正在使用jQuery或Zepto ......

$("body").on("click", "a", function(event) {
   event.target.target != "_blank" && (window.location = event.target.href);
});

答案 19 :(得分:-3)

您可以简单地删除此元标记。

<meta name="apple-mobile-web-app-capable" content="yes">
相关问题