Created
February 9, 2017 13:58
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
((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