-
-
Save cjanis/3908053 to your computer and use it in GitHub Desktop.
Prevent internal links in iOS standalone web apps from opening in Mobile Safari
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
if (window.navigator.standalone) { | |
var local = document.domain; | |
$('a').click(function() { | |
var a = $(this).attr('href'); | |
if ( a.match('http://' + local) || a.match('http://www.' + local) ){ | |
event.preventDefault(); | |
document.location.href = a; | |
} | |
}); | |
} |
And also just flagging that this wouldn't work for urls starting with https://
Some corrections
function escapeRegExp(string){
return string.replace(/([.*+?^${}()|\[\]\/\\])/g, "\\$1");
}
if (window.navigator.standalone) {
var local = document.domain;
$(document).on('click', 'a', function(event) {
var a = $(this).attr('href');
if ( a.match('https?://(www\\.)?' + escapeRegExp(local))){
event.preventDefault();
document.location.href = a;
}
});
}
Note document shouldn't be quoted and event should be added to the function signature.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It would be better to delegate the events, then you will only attach one event handler, instead of multiple.
Change it to: