Skip to content

Instantly share code, notes, and snippets.

@kaelig
Created January 10, 2012 10:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kaelig/1588429 to your computer and use it in GitHub Desktop.
Save kaelig/1588429 to your computer and use it in GitHub Desktop.
Open external links and PDFs in a new window or tab
$(document).ready(function() {
// Open external links in a new window or tab
$(document).on('click', 'a[rel$="external"]', function() {
$(this).attr('target', "_blank");
});
$(document).on('click', 'a[href$=".pdf"]', function() {
$(this).attr('target', "_blank");
});
// Open all urls that don't belong to our domain in a new window or tab
$(document).on('click', "a[href^='http:']:not([href*='" + window.location.host + "'])", function() {
$(this).attr("target", "_blank");
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment