javascript實現(xiàn)阻止iOS APP中的鏈接打開Safari瀏覽器
更新時間:2014年06月12日 10:21:50 投稿:junjie
這篇文章主要介紹了javascript實現(xiàn)阻止iOS APP中的鏈接打開Safari瀏覽器,這個IOS APP一般是Web APP,否則沒法使用本文的代碼,需要的朋友可以參考下
上次根據網上的教程給自己的網站弄了一個Web APP,但是給用戶的感覺卻十分糟糕。
問題說明:
怎么了?原來是打開WEB APP后在主頁上隨意打開連接,就會自作主張地打開Safari瀏覽器。原來好好的偽裝和心情就全被破壞掉了。這該如何是好?原來解決方法十分簡單。僅僅加入這些代碼就好了。實驗測試在本人的 iPhone (iOS 7.1)和iPod (iOS 6.1.4)上測試通過,根據原作者的敘述,最新的 iOS 7.0.4(iPhone 與 iPad)測試通過,代碼應該兼容性不錯,在這里分享:
問題解決:
復制代碼 代碼如下:
<script type=“text/javascript”>
//iOS Web APP中點擊鏈接跳轉到Safari 瀏覽器新標簽頁的問題
if ((“standalone” in window.navigator) && window.navigator.standalone) {
var noddy, remotes = false;
document.addEventListener(‘click',
function(event) {
noddy = event.target;
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>
建議將代碼放到/head標簽前,當然,另外存為一個js 文件引用也是可以的。
相關文章
js實現(xiàn)鍵盤操作實現(xiàn)div的移動或改變的原理及代碼
實現(xiàn)鍵盤操作實現(xiàn)div的移動,最關鍵的一點:獲取div對象,下面有個不錯的示例,大家可以參考下2014-06-06