Skip to content

Instantly share code, notes, and snippets.

@ekka21
Created August 1, 2012 14:46
Show Gist options
  • Save ekka21/3227450 to your computer and use it in GitHub Desktop.
Save ekka21/3227450 to your computer and use it in GitHub Desktop.
jQuery: alert box when leaving the site
// If the link being clicked is external the user will be prompted
// to confirm they wish to leave the site. Confirmation results in
// the external link being opened in a new window
$('a')
.bind('click', function(event) {
var a = new RegExp('/' + window.location.host + '/');
if(!a.test(this.href)) {
event.preventDefault();
var confirmLeave = confirm("You are now leaving the site.");
if(confirmLeave) {
window.open(this.href, '_blank');
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment