Last active
January 29, 2020 14:46
-
-
Save macariojames/753efb72a54e0af69d7e3345c5d4c99f to your computer and use it in GitHub Desktop.
Add icon to all external links
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
/* Add 'text-external' class and rel="external noopener noreferrer" to anchors that are external links | |
* (this.hostname !== location.hostname) but NOT to anchors containing images or subsequent parameters. | |
* Adds a target="_blank" to all external links, so no need to manually write it in | |
* Notice multiple parameters for .not() ~mj */ | |
function addImageToExternalLinks() { | |
$("a").filter(function() { | |
//console.log('Inside addImagetoExternalLinks()') | |
//console.log('this.hostname: ' + this.hostname) | |
//console.log('location.hostname: ' + location.hostname) | |
return this.hostname && this.hostname !== location.hostname; | |
}).not('a:has(img), a:has("span.icon"), .crunchify-link') | |
.attr("rel","external noopener noreferrer") | |
.attr("target", "_blank") | |
.addClass('text-external'); | |
}; // end addImageToExternalLinks() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment