Skip to content

Instantly share code, notes, and snippets.

@josephschmitt
Forked from irae/_Stay_standalone.md
Created June 23, 2011 14:13
Show Gist options
  • Save josephschmitt/1042612 to your computer and use it in GitHub Desktop.
Save josephschmitt/1042612 to your computer and use it in GitHub Desktop.
Prevent links in standalone web apps opening Mobile Safari
<!DOCTYPE html>
<html>
<head>
<title>stay standalone</title>
<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() {
// prevents links from apps from oppening in mobile safari
// this javascript must be the first script in your <head>
if (('standalone' in window.navigator) && window.navigator.standalone) {
var curnode, stop=/^(a|body)$/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 && ( !(/^http/).test(curnode.href) || curnode.href.indexOf(document.location.host)!=-1 ) ) {
e.preventDefault();
document.location.href = curnode.href;
}
},false);
}
})();
</script>
</head>
<body>
<p><a href="http://google.com/">google</a></p>
<script type="text/javascript" charset="utf-8">
// NEVER user document.write, unless for test porposes.
document.write('<p><a href="http://'+document.location.host+'/test/">Same domain</a></p>')
</script>
<p><a href="/test/"><span>absolute path</span></a></p>
<p><a href="test/">relative path</a></p>
<p><a href="#test">anchor</a></p>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment