Skip to content

Instantly share code, notes, and snippets.

@thedaviddias
Forked from kaelig/application.js
Created December 3, 2012 13:38
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thedaviddias/4195094 to your computer and use it in GitHub Desktop.
Save thedaviddias/4195094 to your computer and use it in GitHub Desktop.
JQUERY: 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");
});
});
@Pkushsen
Copy link

$(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