Skip to content

Instantly share code, notes, and snippets.

@irae
Forked from kylebarrow/example.html
Last active January 29, 2024 12:38
Show Gist options
  • Save irae/1042167 to your computer and use it in GitHub Desktop.
Save irae/1042167 to your computer and use it in GitHub Desktop.
Stay Standalone: Prevent links in standalone web apps opening Mobile Safari

#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: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>
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.
@irae
Copy link
Author

irae commented Mar 4, 2020

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.

@wobility
Copy link

wobility commented Mar 4, 2020

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; } } ...

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