Skip to content

Instantly share code, notes, and snippets.

@bruno
Forked from irae/_Stay_standalone.md
Created February 12, 2012 22:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bruno/1811212 to your computer and use it in GitHub Desktop.
Save bruno/1811212 to your computer and use it in GitHub Desktop.
Prevent links in standalone web apps opening Mobile Safari
(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}if("href"in d&&(chref=d.href).replace(e.href,"").indexOf("#")&&!d.attributes.getNamedItem("data-remote")&&(!/^[a-z\+\.\-]+:/i.test(chref)||chref.indexOf(e.protocol+"//"+e.host)===0)){a.preventDefault();e.href=d.href}},false)}})(document,window.navigator,"standalone")
<!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 && // is a link
(chref=curnode.href).replace(location.href,'').indexOf('#') && // is not an anchor
(!(curnode.attributes.getNamedItem('data-remote'))) && // does not contain the data-remote attribute used by jquery-ujs
( !(/^[a-z\+\.\-]+:/i).test(chref) || // either does not have a proper scheme (relative links)
chref.indexOf(location.protocol+'//'+location.host)===0 ) // or is in the same protocol and domain
) {
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="javascript:alert('alerts should work')">alert</a></p>
<p><a href="test_page.html">relative path</a></p>
<p><a href="/test?http://othersite.com">http not on beginning</a></p>
<p><a href="#test">anchor</a></p>
<p><a href="tel:+1234512345">This link fires a call</a></p>
<p><a href="mailto:me@example.com">Mailto</a></p>
</body>
</html>
<!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 src="compressed.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<p><a href="javascript:location.href=document.referrer">javascript:location.href=document.referrer</a></p>
<p><a href="javascript:history.go(-1)">THIS DON'T WORK: javascript:history.go(-1)</a></p>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment