Created
August 1, 2012 14:46
-
-
Save ekka21/3227450 to your computer and use it in GitHub Desktop.
jQuery: alert box when leaving the site
This file contains hidden or 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 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