Last active
August 15, 2020 07:23
-
-
Save wpscholar/4637176 to your computer and use it in GitHub Desktop.
Open all external links in a new window
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
/** | |
* Open all external links in a new window | |
*/ | |
jQuery(document).ready(function($) { | |
$('a') | |
.filter('[href^="http"], [href^="//"]') | |
.not('[href*="' + window.location.host + '"]') | |
.attr('rel', 'noopener noreferrer') | |
.attr('target', '_blank'); | |
}); |
Updated gist to support "noopener noreferrer". See https://medium.com/@jitbit/target-blank-the-most-underestimated-vulnerability-ever-96e328301f4c for details.
Assuming I can add "nofollow" to the rel attribute as well. Could you give me an example script on how to add an exception, for when the A tag has class=trusted? Where only the target attribute is set and the rel attribute is left empty. Many thanks and happy new year!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice gist, @wpscholar. I wrote a jQuery plugin for this that includes several options for customizing the way external links and files are handled.
https://github.com/YupItsZac/jQuery.exLink