iPhone Safari Web应用程序在新窗口中打开链接
-
04-10-2019 - |
题
将图标添加到主屏幕后,我的网络有问题。如果网络是从主屏幕启动的,所有链接将在Safari的新窗口中打开(并丢失全屏功能)。我该如何防止它?我找不到任何帮助,只有相同的未解决问题。
解决方案
我在 iwebkit 框架:
var a=document.getElementsByTagName("a");
for(var i=0;i<a.length;i++)
{
a[i].onclick=function()
{
window.location=this.getAttribute("href");
return false
}
}
其他提示
这里的其他解决方案要么不考虑外部链接(您可能想在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;
}
});
}
});
这是一个古老的问题,这里的许多解决方案都使用JavaScript。从那以后,iOS 11.3已发布,您现在可以使用 范围会员. 。范围成员是一个URL "/"
该范围下的所有路径都不会打开新页面。
范围成员是一个代表此Web应用程序应用程序上下文的导航范围的字符串。
这是我的示例:
{
"name": "Test",
"short_name": "Test",
"lang": "en-US",
"start_url": "/",
"scope": "/",
...
}
您也可以阅读更多有关它的信息 这里. 。我也建议使用 发电机 它将提供此功能。
如果您指定范围,则一切都按预期工作类似,类似于Android,范围内的目的地将在Safari中打开 - PWA的后退按钮(状态栏中的小按钮)。
根据Davids的答案和Richards评论,您应该执行域检查。否则,您的Web应用程序也将打开指向其他网站的链接。
$('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'属性时体验新窗口。实际上,每当$ .mobile.ajaxenabled设置或通过具有target =''属性的$ .mobile.ajaxenabled设置,每当ajaxenabled被关闭,通过和外部链接时,这种情况就会发生。
您可以使用此操作来修复它:
$("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']。
这花了我很长时间才弄清楚,因为我期望它是一个特定于移动的问题,实际上,正是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应用程序元标记
也许当目标明确设置为“ _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进行测试:
您也可以几乎正常地链接:
<a href="#" onclick="window.location='URL_TO_GO';">TEXT OF THE LINK</a>
您可以删除哈希标签和href,它所做的一切都会影响外观。
这就是对我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>
这是Sean's的略微调整版本,它阻止了后退按钮
// 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;
}
});
}
});
对于那些带有twitter引导和铁轨的人3
$('a').live('click', function (event) {
if(!($(this).attr('data-method')=='delete')){
var href = $(this).attr("href");
event.preventDefault();
window.location = href;
}
});
删除链接仍在使用这种方式。
我更喜欢打开独立Web应用模式内的所有链接,除了具有目标=“ _空白”的链接。当然,使用jQuery。
$(document).on('click', 'a', function(e) {
if ($(this).attr('target') !== '_blank') {
e.preventDefault();
window.location = $(this).attr('href');
}
});
我用于iOS Web应用程序的一个解决方法是,我制作了所有链接(由CSS通过按钮)表单表单。因此,我打开了发布到目标链接的表格,然后输入类型=“提交”不是最好的方法,但这是我在找到此页面之前发现的。
我创建了一个可安装的bower套件 @rmarscher的答案 可以在这里找到:
http://github.com/stylr/iosweblinks
您可以使用Bower轻松安装摘要 bower install --save iosweblinks
对于那些使用的人 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');
});
这是我用于页面上所有链接的方法...
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);
});
您可以简单地删除此元标记。
<meta name="apple-mobile-web-app-capable" content="yes">