Created
January 21, 2022 15:02
-
-
Save thierrypigot/268e68b05b510dc583890039d0bbbda7 to your computer and use it in GitHub Desktop.
jQuery add target="_blank" for outgoing link and pdf files
This file contains 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
(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