Forked from wpscholar/jquery.external-links-new-window.js
Last active
December 18, 2019 11:49
-
-
Save rungta/461e738f9f40e97c349f62e613e51f9b to your computer and use it in GitHub Desktop.
Open all external links in a new window
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
/** | |
* Open all external links in a new window | |
*/ | |
(function ($) { | |
function openExternalLinksInNewWindows() { | |
$('a') | |
.filter('[href^="http"], [href^="//"]') | |
.not('[href*="' + window.location.host + '"]') | |
.attr('rel', function (i, val) { | |
return 'noopener noreferrer ' + (val || ''); | |
}) | |
.attr('target', '_blank'); | |
} | |
if (typeof InstantClick !== 'undefined') { | |
InstantClick.on('change', openExternalLinksInNewWindows); | |
} else { | |
$(openExternalLinksInNewWindows); | |
} | |
})(this.jQuery); |
Added InstantClick (a PJAX library) support.
Hi @rungta ,
THX 4 this script!
But if i use it, i get an error "Uncaught ReferenceError: InstantClick is not defined".
I change the line 15 into "(typeof InstantClick != "undefined")" and it works for me.
best regards
Fixed it. Thanks @cn-tools!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated to preserve existing
rel
attribute value (as opposed to overwriting it).