apple-mobile-web-app-capable faq links open in safari

417 Views Asked by At

I've spent most the day adding the mobile-web-app-capable into my site. All now works - links stay in the app and do not open in safari, the page with youtube embedded video now opens BUT the FAQ page division jump links still open in safari... the only ones that do now.

I've used the code below.

<meta name="apple-mobile-web-app-capable" content="yes">

<script type="text/javascript">
    (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');
</script>

My jump link code is:

<span class="faq-link"><a href="#faq-1">Where are...?</a></span>

As soon as you click the question at the top Safari opens.

TIA for your help.

1

There are 1 best solutions below

0
Derek On

Whilst this question was asked seven years ago, I see it has a lot of views. Indeed I found it myself looking for a solution to linked pages of the same site opening in Safari instead of the app.

Rather than use the JavaScript function in the original question I found the solution that worked for me was to include a link to a manifest file in the page header, and add a basic manifest file.

<LINK REL="manifest" HREF="Manifest.json">

The manifest file looked like this...

{
  "name": "A Long Site Name",
  "short_name": "A short name",
  "start_url": "yourWebSite.html",
  "prefer_related_applications": false,
  "display": "standalone",
  "background_color": "#a2c3cb",
  "icons": [{
    "src": "https://www.yourWebsite/apple-touch-icon-196x196.png",
    "sizes": "196x196",
    "type": "image/png"
  }]
}

This will not solve the issue for external links however.