Skip to content

Commit

Permalink
Prevent opening Safari when navigating in iOS standalone mode
Browse files Browse the repository at this point in the history
Links are set to open in an external browser, instead of navigating
the current view. This function loads the new page in the same full-screen
window, thereby keeping people on the same page.
  • Loading branch information
francois committed Aug 5, 2016
1 parent f9cd9a3 commit 962a336
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions views/layout.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,28 @@
<link rel="stylesheet" href="/css/app.css">
<title>No Frills Sleep Tracker<%= @human_stage %></title>
<script type="text/javascript" src="/js/bundle.js"></script>
<script type="text/javascript">
// copied from https://gist.github.com/kylebarrow/1042026#gistcomment-37145
(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;
}
// Conditions 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) {
e.preventDefault();
location.href = curnode.href;
}
}, false);
}
})(document, window.navigator, 'standalone');
</script>
</head>
<body>
<%= yield %>
Expand Down

0 comments on commit 962a336

Please sign in to comment.