Skip to content

Instantly share code, notes, and snippets.

@imagehat
Last active December 16, 2015 17:08
Show Gist options
  • Save imagehat/5467788 to your computer and use it in GitHub Desktop.
Save imagehat/5467788 to your computer and use it in GitHub Desktop.
Open external links and PDFs in a new window
// Open external links and PDFs in a new window
$("a[href^='http'], a[href$='.pdf']").each(function() {
if(this.href.indexOf(location.hostname.replace(/www./gi, "")) == -1 || this.href.lastIndexOf('.pdf') !== -1) {
// Ensure subdomains are not counted as external
if(this.href.indexOf('mywebsite.com') == -1) {
$(this).click(function() {
window.open(this.href, "_blank");
return false;
});
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment