我有这个代码,我抓住某个地方让我的独立iOS网络应用程序在应用程序中导航时保持在自身内部:
(function(document, navigator, standalone) {
// prevents links from apps from oppening in mobile safari
// this javascript must be the first script in your <head>
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;
}
// Condidions to do this only on links to your own app
// if you want all links, use if('href' in curnode) instead.
if ('href' in curnode && (curnode.href.indexOf('http') || ~curnode.href.indexOf(location.host)) && e.defaultPrevented !== true) {
e.preventDefault();
location.href = curnode.href;
}
}, false);
}
})(document, window.navigator, 'standalone');
这基本上可以防止应用程序进入后台并在应用程序中打开Safari中的链接。
我需要一个例外,所以当我放置一个target =“_ blank”或一个rel =“external”属性时,它实际上必须在Safari中打开(而WEb-App会转到后台)。
我尝试在第15行之前放置if语句,如下所示:
(function(document, navigator, standalone) {
// prevents links from apps from oppening in mobile safari
// this javascript must be the first script in your <head>
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 (e.target.getAttribute('rel') == 'external') {
window.open("http://www.google.com");
} else {
// Condidions to do this only on links to your own app
// if you want all links, use if('href' in curnode) instead.
if ('href' in curnode && (curnode.href.indexOf('http') || ~curnode.href.indexOf(location.host)) && e.defaultPrevented !== true) {
e.preventDefault();
location.href = curnode.href;
}
}
}, false);
}
})(document, window.navigator, 'standalone');
但是不起作用......
答案 0 :(得分:0)
这是解决方案:
(function(document, navigator, standalone) {
// prevents links from apps from oppening in mobile safari
// this javascript must be the first script in your <head>
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 (curnode.getAttribute('rel') == 'external') {
window.open("http://www.google.com");
} else {
// Condidions to do this only on links to your own app
// if you want all links, use if('href' in curnode) instead.
if ('href' in curnode && (curnode.href.indexOf('http') || ~curnode.href.indexOf(location.host)) && e.defaultPrevented !== true) {
e.preventDefault();
location.href = curnode.href;
}
}
}, false);
}
})(document, window.navigator, 'standalone');