Skip to main content

Prevent iOS standalone mode web apps from opening links in Safari browser.

// Prevent iOS Standalone Mode Web Apps From Opening Links in Safari
// 
// Add to HTML <head>:
// <meta name="apple-mobile-web-app-capable" content="yes">
//
// IMPORTANT: js script must be in the HTML <head> tag and cannot be
// externally linked. - jon labelle, 1/8/14
//
// Reference:
// http://stanislav.it/how-to-prevent-ios-standalone-mode-web-apps-from-opening-links-in-safari/
//
<meta name="apple-mobile-web-app-capable" content="yes">
<script>
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>

// minified
// -----------------------

<!-- prevent links from opening in mobile safari -->
<script type="text/javascript">(function(a,b,c){if(c in b&&b[c]){var d,e=a.location,f=/^(a|html)$/i;a.addEventListener("click",function(a){d=a.target;while(!f.test(d.nodeName))d=d.parentNode;"href"in d&&(d.href.indexOf("http")||~d.href.indexOf(e.host))&&(a.preventDefault(),e.href=d.href)},!1)}})(document,window.navigator,"standalone")</script>