javascript實現阻止iOS APP中的連結開啟Safari瀏覽器_javascript技巧

來源:互聯網
上載者:User

上次根據網上的教程給自己的網站弄了一個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 檔案引用也是可以的。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.