Links in iOS Safari WebApp keeps opening in a new tab

When you use the <meta name="apple-mobile-web-app-capable" content="yes"> tag in your HTML the links in your app with open in a new tab in Safari. This is something you usually don’t want.

Put this as the first script in your <head>:

if (('standalone' in navigator) && navigator.standalone) {
  document.addEventListener('click', function(e) {
    var curnode =
    while (!(/^(a|html)$/i).test(curnode.nodeName)) {
      curnode = curnode.parentNode
    if ('href' in curnode
      && (chref = curnode.href).replace(document.location.href, '').indexOf('#')
      && (!(/^[a-z\+\.\-]+:/i).test(chref)
      || chref.indexOf(document.location.protocol + '//' + === 0)
    ) {
      document.location.href = curnode.href
  }, false)

It opens all you own links in the same app window and other links in a new tab in Safari.

This code will not work with javascript:history.go(-1).


Happy coding! – Found a mistake or a typo? Please submit a PR to my GitHub-repo.

Like this post? Follow @adriaanvrossum on Twitter