External Link Warning
HTML
<a href="https://www.fiddle.jshell.net/test.html">Absolute Local Link</a><br>
<a href="/test.html">Relative Local Link</a><br>
(these local links don't go to real pages, FYI)
<br>
<a href="mailto:[email protected]">Mailto Link</a>
<hr>
<a href="https://www.google.com">External Link</a>
JavaScript
/* EXTERNAL LINK WARNING
=========================================*/
$('a').on('click', function(e){
e.preventDefault();
var url = $(this).attr('href'),
host = location.host;
if (url.indexOf(host) > -1 || url.indexOf('http','https') == -1){
/* If we find the host name within the URL,
OR if we do not find http or https,
meaning it is a relative internal link
*/
window.location.href = url;
} else {
var warn = confirm('You\'re leaving the domain.\n\nAre you sure?');
if(warn == true) {
window.location.href = url,'_blank';
} else {
e.preventDefault;
}
}
});