[英]Force link to open in mobile safari from a web app with javascript
[英]Open a link from web app to new Safari window in iOS 8
更新07.14.15失去所有希望......我假設我的修復程序將與javascript相關,但我根本不知道。 我當前的代碼是否停止了我的HTML目標=“空白”?
我正在制作一個在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中打開其他鏈接?
這個問題如何從iOS 7中的WebApp打開Safari與我的相似,但修復程序對我來說不起作用,我現在在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應用程序下的本地鏈接仍然在同一窗口中打開,但外部http://鏈接將在Safari中打開
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.