在 iOS 8 中从网络应用程序打开 link 到新的 Safari window
Open a link from web app to new Safari window in iOS 8
更新 07.14.15
失去所有希望...
我假设我的修复将与 javascript 相关,但我根本不知道。我当前的代码是否阻止我的 HTML target="Blanks" 工作?
我正在制作一个 HTML/CSS 应用程序,可以在 iPad Mini 上下载。这是一个包含文本和图像的 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;
}
});
}
});
很好,很棒。我希望该网站能够独立运行 window,看起来像一个本地应用程序。
现在我需要让这个 link 在 Safari 中打开(它必须打开 Safari 并切换到 window)。
target="_blank" 不起作用,或者 rel="external"
如何让 link 在网络应用程序中打开,而其他 link 在 Safari 中打开?
这个问题 How to open Safari from a WebApp in iOS 7 和我的很相似,但是修复对我不起作用,我现在在 iOS 8。
我找到答案了!从这里 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>
这样,Web 应用程序下的本地链接仍会在相同的 window 中打开,但外部 http:// 链接将在 Safari 中打开
更新 07.14.15 失去所有希望... 我假设我的修复将与 javascript 相关,但我根本不知道。我当前的代码是否阻止我的 HTML target="Blanks" 工作?
我正在制作一个 HTML/CSS 应用程序,可以在 iPad Mini 上下载。这是一个包含文本和图像的 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;
}
});
}
});
很好,很棒。我希望该网站能够独立运行 window,看起来像一个本地应用程序。
现在我需要让这个 link 在 Safari 中打开(它必须打开 Safari 并切换到 window)。
target="_blank" 不起作用,或者 rel="external"
如何让 link 在网络应用程序中打开,而其他 link 在 Safari 中打开?
这个问题 How to open Safari from a WebApp in iOS 7 和我的很相似,但是修复对我不起作用,我现在在 iOS 8。
我找到答案了!从这里 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>
这样,Web 应用程序下的本地链接仍会在相同的 window 中打开,但外部 http:// 链接将在 Safari 中打开