Created
September 10, 2015 19:32
-
-
Save cscott530/66914f527d6dfe8868f9 to your computer and use it in GitHub Desktop.
Force all external links to open into a new tab
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
$(function() { | |
$('body').on('click', 'a', function(e) { | |
//host will be empty if it's like a <a href="#"> type tag that has no path | |
//those should execute | |
//if host is set, and different than current, force to a new tab. | |
if (e.target.host && e.target.host != location.host) { | |
console.log('Opening external link'); | |
e.preventDefault(); | |
window.open(e.target.href, '_blank'); | |
return false; | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment