Skip to content

Instantly share code, notes, and snippets.

@gorlandor
Created February 9, 2017 13:58
Show Gist options
  • Save gorlandor/ab20d7cf4a3d44fbbedf3127ed9ea8ac to your computer and use it in GitHub Desktop.
Save gorlandor/ab20d7cf4a3d44fbbedf3127ed9ea8ac to your computer and use it in GitHub Desktop.
Allows for in-app navigation for add-to-home-screen capable web apps on iOS. Prevents links from opening in a new window on mobile Safari. Add this as the first script tag inside the head. Link to irae's gist: https://gist.github.com/irae/1042167
((document, navigator, standalone) => {
'use strict';
if ((standalone in navigator) && navigator[standalone]) {
let currentNode, location = document.location, stop = /^(a|html)$/i;
let onClickHandler = (event) => {
currentNode = event.target;
while (!(stop).test(currentNode.nodeName)) {
currentNode = currentNode.parentNode;
}
if ('href' in currentNode
&& (currentNode.href.indexOf('http') || currentNode.href.indexOf(location.host))) {
event.preventDefault();
location.href = currentNode.href;
}
};
document.addEventListener('click', onClickHandler, false);
}
console.debug({'App': 'init'});
})(document, window.navigator, 'standalone');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment