Prevent links in standalone web apps opening Mobile Safari


/ Published in: HTML
Save to your folder(s)

Prevent links in standalone web apps opening Mobile Safari


Copy this code and paste it in your HTML
  1. <!DOCTYPE html>
  2. <title>stay standalone</title>
  3. <meta name="apple-mobile-web-app-capable" content="yes">
  4. <meta name="viewport" content="width=device-width,initial-scale=1.5,user-scalable=no">
  5. <script type="text/javascript">
  6. (function(document,navigator,standalone) {
  7. // prevents links from apps from oppening in mobile safari
  8. // this javascript must be the first script in your <head>
  9. if ((standalone in navigator) && navigator[standalone]) {
  10. var curnode, location=document.location, stop=/^(a|html)$/i;
  11. document.addEventListener('click', function(e) {
  12. curnode=e.target;
  13. while (!(stop).test(curnode.nodeName)) {
  14. curnode=curnode.parentNode;
  15. }
  16. // Condidions to do this only on links to your own app
  17. // if you want all links, use if('href' in curnode) instead.
  18. if('href' in curnode && ( curnode.href.indexOf('http') || ~curnode.href.indexOf(location.host) ) ) {
  19. e.preventDefault();
  20. location.href = curnode.href;
  21. }
  22. },false);
  23. }
  24. })(document,window.navigator,'standalone');
  25. </script>
  26. </head>
  27. <p><a href="http://google.com/">google</a></p>
  28. <script type="text/javascript" charset="utf-8">
  29. // NEVER user document.write, unless for test porposes.
  30. document.write('<p><a href="http://'+document.location.host+'/test/">Same domain</a></p>')
  31. </script>
  32. <p><a href="/test/"><span>absolute path</span></a></p>
  33. <p><a href="test/">relative path</a></p>
  34. <p><a href="/test?http://othersite.com">http not on beginning</a></p>
  35. <p><a href="#test">anchor</a></p>
  36. </body>
  37. </html>

URL: https://gist.github.com/1042167/fcd09c2e0fa9af91796d65992c785019ec0681fc

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.