iOS Safari standalone in-webapp navigation suddenly not working

978 Views Asked by At

In iOS its standalone webapp modus you can prevent a link being opened in Mobile Safari instead of opening in the webapp itself:

(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 && // is a link
            (chref=curnode.href).replace(location.href,'').indexOf('#') && // is not an anchor
            (   !(/^[a-z\+\.\-]+:/i).test(chref) ||                       // either does not have a proper scheme (relative links)
                chref.indexOf(location.protocol+'//'+location.host)===0 ) // or is in the same protocol and domain
        ) {
            e.preventDefault();
            location.href = curnode.href;
        }
    },false);
}
})(document,window.navigator,'standalone');

This has been working for a while now but since Apple's new iOS update each link is openend in Mobile Safari.

After debugging I have discovered that the conditions in the code are still correct because in standalone mode the location.href = curnode.href; is still being hit/executed.

So I think: changing the document it's href which loads the page for the link that's been clicked on does not work anymore (stricter rules or so?)

I have tried to change the pathname instead of the full url by doing:
location.pathname = curnode.getAttribute('path');
(Thinking that if only the path is changed maybe the webapp will stay in webapp)

Is there anyone out there also facing this problem or someone who can help me think in the right direction for a solution or, better but less fun, has the solution?

1

There are 1 best solutions below

0
nelsonvarela On

Found the solution. Instead of using location.href = curnode.href; I now use location.assign(curnode.href);