Created
April 23, 2012 15:41
-
-
Save dfadler/2471754 to your computer and use it in GitHub Desktop.
Confirm external link and open in a new window
This file contains 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