Skip to content

Instantly share code, notes, and snippets.

@StuPig
Last active December 26, 2015 20:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save StuPig/7208352 to your computer and use it in GitHub Desktop.
Save StuPig/7208352 to your computer and use it in GitHub Desktop.
Prevent links in standalone web apps opening Mobile Safari. Reference: https://gist.github.com/kylebarrow/1042026#comment-37145 https://gist.github.com/irae/1042167
// add this inlined to <head> and before all <links>
<script>
(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>
// add this inline to <head> and before all <links>
<script>
(function(a,b,c){if(c in b&&b[c]){var d,e=a.location,f=/^(a|html)$/i;a.addEventListener("click",function(a){d=a.target;while(!f.test(d.nodeName))d=d.parentNode;"href"in d&&(chref=d.href).replace(e.href,"").indexOf("#")&&(!/^[a-z\+\.\-]+:/i.test(chref)||chref.indexOf(e.protocol+"//"+e.host)===0)&&(a.preventDefault(),e.href=d.href)},!1)}})(document,this.navigator,"standalone");
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment