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

将图标添加到主屏幕后,我遇到了问题。 如果网页从主屏幕启动,则所有链接将在Safari的新窗口中打开(并失去全屏function)。 我怎样才能防止它? 我找不到任何帮助,只有同样的未答复的问题。

我在iWebKit框架中find了JavaScript解决scheme:

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

这里的其他解决scheme或者不考虑外部链接(你可能想在Safari中从外部打开)或者不考虑相对链接(没有在其中的域)。

html5移动样板项目链接到这个主题上有一个很好的讨论主题: 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> 

如果你使用jQuery,你可以这样做:

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

这是在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;      }    });    } }); 

根据Davids的回答和Richards的评论,你应该进行域名检查。 否则,其他网站的链接也将在您的networking应用程序中打开。

 $('a').live('click', function (event) { var href = $(this).attr("href"); if (href.indexOf(location.hostname) > -1) { event.preventDefault(); window.location = href; } }); 

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

你可以用下面的方法修复它:

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

(感谢理查德·普尔的live()方法 – 没有使用bind())

如果你closures了全局的ajaxEnabled,你将需要删除[data-ajax ='false']。

这花了我很长的时间,因为我期待它是一个jQuery Mobile的具体问题,其实它是实际上禁止新窗口的Ajax链接。

此代码适用于iOS 5(它为我工作):

在标签中:

 <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

也许你应该允许打开目标被明确设置为“_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; } }); 

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

保持独立:防止独立Web应用程序中的链接打开Mobile Safari

这是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> 

这是肖恩的稍微修改版本,防止后退button

 // 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; } }); } 

});

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

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

你可以删除哈希标签和HREF,它会影响外观。

对于那些与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; } }); 

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

我更喜欢打开独立Web应用程序模式中除target =“_ blank”之外的所有链接。 当然使用jQuery。

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

我使用的iOS Web应用程序的一个解决方法是,我做所有的链接(这是由CSS的button)表单提交button。 所以我打开了一个表格,发布到目标链接,然后inputtype =“submit”不是最好的方法,但这是我find之前,我发现这个网页。

我在@ rmarscher的答案中创build了一个凉亭可安装包,可以在这里find:

http://github.com/stylr/iosweblinks

您可以使用bower install --save iosweblinks轻松安装带有bower的代码段

对于那些使用JQuery Mobile ,上面的解决scheme会打破popup对话框 这将保持在Web应用程序内的链接,并允许popup窗口。

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

这是我将用于页面上的所有链接…

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