Skip to content

Instantly share code, notes, and snippets.

@tomazursic
Created March 10, 2017 12:04
Show Gist options
  • Save tomazursic/b850f921e8d1b11079ab6b0a20ee3d2f to your computer and use it in GitHub Desktop.
Save tomazursic/b850f921e8d1b11079ab6b0a20ee3d2f to your computer and use it in GitHub Desktop.
Warning when opening external links
(function ($) {
var root = new RegExp(location.host);
$( 'a' ).each(function() {
if( location.hostname === this.hostname || !this.hostname.length ) {
$(this).addClass('local');
} else {
$(this).addClass('external');
}
});
$('a.external').on('click', function(e){
e.preventDefault();
var notify = confirm(
"You are about to leave the website of [COMPANY] and view the content of an external website. " +
"[COMPANY] cannot be held responsible for the content of external websites.\n\n" +
"Destination: " + $(this).attr('href')
);
if (notify){
window.location = $(this).attr('href');
}
});
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment