Return to Snippet

Revision: 54753
at January 10, 2012 20:20 by ichnoweb


Initial Code
<!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(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>
	<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?http://othersite.com">http not on beginning</a></p>
	<p><a href="#test">anchor</a></p>
</body>
</html>

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

Initial Description
Prevent links in standalone web apps opening Mobile Safari

Initial Title
Prevent links in standalone web apps opening Mobile Safari

Initial Tags
mobile

Initial Language
HTML