(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&&(d.href.indexOf("http")||~d.href.indexOf(e.host))&&(a.preventDefault(),e.href=d.href)},!1)}})(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 && ( 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> |
I ended up with this: https://gist.github.com/3908053
It works just fine for me.
Hey guys,
Amazing script, does exactly what I want!
Only problem is, I'm firing a bunch of jQuery events after this is triggered, and their seems to be issues once I use this script.
Any ideas why?
Hey guys, great solution. It works great for me except for the "Pin" button for Pinterest, which is javascript. When you click it, the page just reloads the web app page. Is there a way to allow users to pin in app or send them to safari and have the script ignore javascript links? The first option would be preferable. I'm not versed in javascript, so any help is appreciated. Thanks very much.
Used @emilsall's change to fix the same problem of anchors with no href attributes. I'd also be interested in the 'href' in curnode
reasoning.
Hello
(excuse for my bad englis, i'm french)
Your gist works well on my little webapp, but i'm using a jquery plugin (http://srobbin.github.io/jquery-pageslide/) to display a side and slide menu opened from the left of the screen.
The problem is that all the links inside this left menu are opened outside, i mean in ios Safari...
Could you have a look at my test site : http://sqliwpdev.com/quizz/
thanks
@irae: The script works great, but is it supposed to leak the chref variable into the global scope?
This script is working great for me.... thanks!
found a nasty bug if you use facebook together with this piece of javascript:
change
{code}
(function(document,navigator,standalone) {
// prevents links from apps from oppening in mobile safari
// this javascript must be the first script in your
{code}
to
{code}
(function(document,navigator,standalone) {
// prevents links from apps from oppening in mobile safari
// this javascript must be the first script in your head
{code}
Facebook code search for and insert himself after the tag, this was breaking the javascript
when I use the script it kills the Nivo-Lightbox ( https://github.com/gilbitron/Nivo-Lightbox) launch up and it just opens the image on its own :( Anyone have any ideas? help me please
adding this works to get the naive-lightbox working:-)
(!(curnode.attributes.getNamedItem('data-lightbox-gallery'))) &&
(!(curnode.attributes.getNamedItem('data-remote'))) && // does not contain the data-remote attribute used by jquery-ujs
I had to add this line (chref !== "") &&
after line 22 due to my navigation using a-tags without hrefs.
This is amazing - any chance on the snippet being published as a Bower repo? 😄
This script is not working in iOS 9.3.5 :(
It does not work on Android for me.
Please help.
Thank you so much!
Thanks The script worked for internal links which were opening new pages. But ...
- The virtual keyboard is being a goofball. It keeps popping up and going down while opening the app from the homescreen or when opening a new page for no reason.
Hello,
I was developing a web application using asp.net. I tried the above js and for button clicks, this is working fine. but if I click any anchor button or hyperlink it takes me back to safari browser. any tweaks are highly appreciated.
This doesn't work if the URL is formatted as:
https://user:password@www.site.com
How would one go about fixing this?
This does not work in latest iOS/Safari. Does anyone has a solution already? The location.href is executed but it opens the link in the browser.
@nelsonvarela You might want to check out this gist https://gist.github.com/kylebarrow/1042026
@kongakong thank you so much! I've change location.href = current_node.href
into location.assign(current_node.href);
and works like a charm
@nelsonvarela Do you mind to post your code? I can't get it to work. Need to see more reference to understand what really prevent it from working.
Is there anyone that has a working version of this script, I'd love your help/solution being posted here :-)
Thank you for this great script which I have been using since iOS 11.0.
Unfortunately, tonights iOS 11.3 public beta 1 changed something so each link now opens in the in-app browser (it doesn't jump to Safari but adds the in-app location and toolbar on top of the page.
Would there be a solution to solve his?
Many thanks!
@jwronken good to know the script was valuable to you for so much time. Unfortunately I am not supporting any apps currently and I consider my knowledge of the state of the art a bit outdated. Everyone is calling this PWAs now, and I would check with either the HTML5 Boilerplate guys or read a bit of the Chrome Developer Advocate talks. I suspect there are <meta>
tags and other modern tech to help you with this in modern browsers. Appreciate if you can post here your findings so the community (people following comments on this gist) can benefit.
Hi Irea
I found indeed the solution...in your index.html (or better said, the initial loading page) include this in the header:
<link rel="manifest" href="manifest.json">
<meta name="mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="application-name" content="your app name">
<meta name="apple-mobile-web-app-title" content="shortname">
<meta name="msapplication-starturl" content="index.html">
<script src="compressed.js" type="text/javascript" charset="utf-8"></script>
then create a text file called manifest.json with the following:
{
"name": "your app name",
"short_name": "shortname",
"lang": "en-US",
"start_url": "index.html",
"display": "standalone",
"icons": [
{
"src": "./img/apple-icon-144x144.png"
}
]
}
that's all to get it working, make sure of course path to compressed.js and the images is correct.
More info and the builder to make the manifest.json and header file :
https://tomitm.github.io/appmanifest/
thank again @irae for your script!
Hi,
This original script (to the top works well) Thanks. But I need to use it with input file. It's desactive click on input file for now. Any idea to add input file in the scope please ?
hi and thanks for the script . this prevent some of my modals to open . the modal opens and it closes suddenly any idea how to exclude some links and pages from this script ?? or any other way to fix this .
For both @wobility and @farshadff I would like to remember the goal here.
This small script is meant to help a pure HTML website that wants to be added to the iPhone home screen and not show the Safari interface. If the site is purely HTML and CSS, it is where this small script helps the most. Both the file implementation and the modal are application logic that can be considered out of the scope of this small script and maybe the goals here diverge.
For the input file (from @wobility), maybe letting it open in Safari is a good thing, since it shows the "lock" mark to prove you are using SSL and uploading file is secure. When I created this small utility iOS didn't support input type file, so any contributions are welcome.
For the modal (from @farshadff), try e.stopPropagation()
in your modal link, without changing the script above. If that's not helping, your site might deserve its own solution instead of the script above.
Yes thanks for your answer. Of course the website is a Webapps in Html / JS / CSS. Your script works fine and it is really cool. I finded a simple tricks to open file input, it's not the best way but I had don't time to find better way. I just add an exception with bollean varaible. I forgot to tell you that I was going through a specific design with an Href link, that's why I had this problem. Otherwise the input file works well.
<a href="#" class="photoUpload" onclick="inputFileLink=true;"> <form method="POST" enctype="multipart/form-data" id="fileProfileUploadForm" action="/user/upload_picture/"> <input type="file" accept="image/*" class="upload" name="fileProfileUpload" id="fileProfileUpload" onchange="$('#fileProfileUploadForm').submit();"> </form> </a>
... if ('href' in noddy && noddy.href.indexOf('http') !== -1 && (noddy.href.indexOf(document.location.host) !== -1 || remotes)) { if(inputFileLink){ inputFileLink = false; }else{ event.preventDefault(); document.location.href = noddy.href; } } ...
Hi smart people, I'm running into a problem with my site. This script works wonderfully on the first page of the site — all the links seem to be opening just fine without opening Safari — but anywhere except the home page it's having trouble. I think the problem is that on the other pages some FB social plugin JS is being dynamically inserted above this code. Any idea how to make sure that this script is always the first JS in the header? If you want to take a look at the site: http://craigjanis.net