Skip to content

Instantly share code, notes, and snippets.

@MichaelWhi
Last active January 3, 2016 17:09
Show Gist options
  • Save MichaelWhi/8494381 to your computer and use it in GitHub Desktop.
Save MichaelWhi/8494381 to your computer and use it in GitHub Desktop.
JavaScript Snippet to be put at bottom of the body, which will fix links including rails UJS links for WebApps ran from the iOS home screen.
if(("standalone" in window.navigator) && window.navigator.standalone){
var noddy, remotes = false;
var iosClickFixer = function(event) {
noddy = event.target;
while(noddy.nodeName !== "A" && noddy.nodeName !== "HTML") {
noddy = noddy.parentNode;
}
if('href' in noddy && noddy.href.indexOf('http') !== -1 && (noddy.href.indexOf(document.location.host) !== -1 && noddy.getAttribute("data-method") !== "post" && noddy.getAttribute("data-method") !== "delete" || remotes))
{
event.preventDefault();
if(typeof(Turbolinks) !== "undefined"){
Turbolinks.visit(noddy.href);
} else {
document.location.href = noddy.href;
}
}
};
document.addEventListener('click', iosClickFixer,false);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment