I’m using the following code on a banking website to warn users that they are leaving the website.
$('a').filter(function() {
return this.hostname && this.hostname !== location.hostname;
})
.click(function () {
return window.confirm('Warning message here...');
});
});
Currently when you click on the online banking login you get the popup window. I need to be able to exclude specific URLs that are not part of the website’s domain name so I can exclude the online banking website, but I have no idea how to do it.
Any help is appreciated.
$('body').on('click', 'a', function(e){
if(this.hostname != 'mydomain.com' && this.hostname != 'www.mydomain.com'){
if(!confirm('This is an external link. Are you sure you want to leave?')){
e.preventDefault();
}
}
});
I've used event delegation here to make sure it captures all links (including injected links).
You may want to add mutiple domain conditions if you have sub-domains for example (www.mydomain.com
).