Skip to content

Instantly share code, notes, and snippets.

@thierrypigot
Created January 21, 2022 15:02
Show Gist options
  • Save thierrypigot/268e68b05b510dc583890039d0bbbda7 to your computer and use it in GitHub Desktop.
Save thierrypigot/268e68b05b510dc583890039d0bbbda7 to your computer and use it in GitHub Desktop.
jQuery add target="_blank" for outgoing link and pdf files
(function($) {
jQuery( document ).ready(function($) {
// external links to new window
add_target_blank_to_external_links();
// force PDF Files to open in new window
add_target_blank_to_pdf_links();
});
function add_target_blank_to_external_links(){
$( 'a[href^="http://"], a[href^="https://"]' ).not( 'a[href*="'+ location.hostname +'"]' ).attr( 'target','_blank' );
}
function add_target_blank_to_pdf_links(){
$( 'a[href$=".pdf"]' ).attr( 'target', '_blank' );
}
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment