-
-
Save thedaviddias/4195094 to your computer and use it in GitHub Desktop.
JQUERY: Open external links and PDFs in a new window or tab
This file contains hidden or 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
$(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
$(document).ready(function() {
$(document).on('click', 'a[rel$ ="external"]', function() {
$(document).on('click', 'a[href$ =".pdf"]', function() {
// Open external links in a new window or tab
$(this).attr('target', "_blank");
});
$(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");
});
});