Here is a refinement of Ryan's answer that allows you to block specific paths on your own domain as well as external links.
It checks the host and path seperately - so you can block all links that are not from your own domain, and specific paths on your own domain.
If the user clicks on a blocked link, the "request_confirmation" function requests confirmation from the user before allowing navigation away from the current page.
I have broken the logic into multiple functions so you can modify it easily.
$('body').on('click', 'a', function(e){
return (should_link_be_blocked($(this).attr('href')))? request_confirmation() : true;
});
function should_link_be_blocked(link){
var url = new URL(link);
return should_host_be_blocked(url.hostname) || should_path_be_blocked(url.pathname);
}
function should_host_be_blocked(host){
return !(host.match(/([a-z0-9]+\.)?example.com/));
}
function should_path_be_blocked(path){
var not_allowed = ['/block_me.html', '/block_another.html'];
var block = false;
not_allowed.forEach(function(n_a){
block = (path == n_a)? true : block;
});
return block;
}
function request_confirmation(){
return confirm('This is an external link. Are you sure you want to leave?');
}
Here is an ugly example of it working : http://codepen.io/anon/pen/Gjagi