Skip to content

Instantly share code, notes, and snippets.

@cjanis
Forked from kylebarrow/example.html
Created October 17, 2012 20:44
Show Gist options
  • Save cjanis/3908053 to your computer and use it in GitHub Desktop.
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
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;
}
});
}
@DanielApt
Copy link

And also just flagging that this wouldn't work for urls starting with https://

@jtsay362
Copy link

jtsay362 commented Mar 4, 2015

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