/ Published in: HTML
Prevent links in standalone web apps opening Mobile Safari
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<!DOCTYPE html> <html> <head> <meta name="apple-mobile-web-app-capable" content="yes"> <meta name="viewport" content="width=device-width,initial-scale=1.5,user-scalable=no"> <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 && ( curnode.href.indexOf('http') || ~curnode.href.indexOf(location.host) ) ) { e.preventDefault(); location.href = curnode.href; } },false); } })(document,window.navigator,'standalone'); </script> </head> <body> <script type="text/javascript" charset="utf-8"> // NEVER user document.write, unless for test porposes. </script> </body> </html>
URL: https://gist.github.com/1042167/fcd09c2e0fa9af91796d65992c785019ec0681fc