#Stay Standalone
A short script to prevent internal links to a "webapp" added to iPhone home screen to open in Safari instead of navigating internally.
#Stay Standalone
A short script to prevent internal links to a "webapp" added to iPhone home screen to open in Safari instead of navigating internally.
(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&&(chref=d.href).replace(e.href,"").indexOf("#")&&(!/^[a-z\+\.\-]+:/i.test(chref)||chref.indexOf(e.protocol+"//"+e.host)===0)&&(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 && // is a link | |
(chref=curnode.href).replace(location.href,'').indexOf('#') && // is not an anchor | |
( !(/^[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:[email protected]">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> |
MIT License | |
=========== | |
Copyright (c) 2009–2011 | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in | |
all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
THE SOFTWARE. | |
BSD License | |
=========== | |
Copyright (c) 2011 | |
All rights reserved. | |
Redistribution and use in source and binary forms, with or without | |
modification, are permitted provided that the following conditions are met: | |
* Redistributions of source code must retain the above copyright | |
notice, this list of conditions and the following disclaimer. | |
* Redistributions in binary form must reproduce the above copyright | |
notice, this list of conditions and the following disclaimer in the | |
documentation and/or other materials provided with the distribution. | |
* Neither the name of the Organization nor the | |
names of its contributors may be used to endorse or promote products | |
derived from this software without specific prior written permission. | |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | |
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER BE LIABLE FOR ANY | |
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | |
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
@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; } } ...
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.