Skip to content

Instantly share code, notes, and snippets.

Created June 23, 2011 06:30
Show Gist options
  • Save kylebarrow/1042026 to your computer and use it in GitHub Desktop.
Save kylebarrow/1042026 to your computer and use it in GitHub Desktop.
Prevent links in standalone web apps opening Mobile Safari
<!DOCTYPE html>
<title>Stay Standalone</title>
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<script src="stay_standalone.js" type="text/javascript"></script>
<li><a href="">Remote Link (Google)</a></li>
<li><a href="javascript:alert('Awesome script is awesome')">JavaScript Link</a></li>
<li><a href="/">Local Link</a></li>
<li><a href="#amp">Local Anchor</a></li>
// Mobile Safari in standalone mode
if(("standalone" in window.navigator) && window.navigator.standalone){
// If you want to prevent remote links in standalone web apps opening Mobile Safari, change 'remotes' to true
var noddy, remotes = false;
document.addEventListener('click', function(event) {
noddy =;
// Bubble up until we hit link or top HTML element. Warning: BODY element is not compulsory so better to stop on HTML
while(noddy.nodeName !== "A" && noddy.nodeName !== "HTML") {
noddy = noddy.parentNode;
if('href' in noddy && noddy.href.indexOf('http') !== -1 && (noddy.href.indexOf( !== -1 || remotes))
document.location.href = noddy.href;
Copy link

@khovanskiy I think you forgot to include the Utils.isRelativePath(href)function used in your code snippet:

const isRelativePath = Utils.isRelativePath(href);

Can you please include it to complete the solution you were sharing?

Copy link

Could someone please help me with similar code for a HTML form post ?

Copy link

aaronhb commented May 10, 2019

I am trying to find a solution so that i can have it open PDF files or other links open in mobile web browser but all others stay in the PWA. Anyone know how to do this?

I am using the following code

<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){;while(!f.test(d.nodeName))d=d.parentNode;"href"in d&&(d.href.indexOf("http")||~d.href.indexOf(,e.href=d.href)},!1)}})(document,window.navigator,"standalone")</script>

Copy link

ssfinney commented Jul 1, 2019

Works for me!

Copy link

SarKaa commented Nov 2, 2019

Is there any way to make this work on iframes. I mean all links opened within the iframe open in the web app?

Copy link

This solution appears to launch URLs in a lightbox from the Web App in iOS 13.2. Is anyone else seeing this behavior?

Copy link

@justinputney Yes, seeing same thing here.

Copy link

@johnbarratt Thanks for the confirmation. Let me know if you find a fix where it doesn't trigger a ligthbox.

Copy link

@justinputney : Just found this, seems to fix it, you just need a bare bones manifest, though it only works for pages added after the manifest was added to the site :

Copy link

@johnbarratt Thanks!

Copy link

joe182 commented Feb 18, 2021

correct me if I'm wrong, but to prevent links from opening in safari, isn't just the scope the only thing you need to add to your manifest? No javascript, no hacks no nothing just a single line in the json file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment