Last active
September 28, 2022 09:46
-
-
Save mlent/60010ba2bb46c5bcad8af114d283c939 to your computer and use it in GitHub Desktop.
open-links-in-new-tab.js
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
<script type="text/javascript"> | |
jQuery(document).ready(function($){ | |
$('a').each(function(){ | |
const href = $(this).attr('href'); | |
if (!href) { | |
return; | |
} | |
const isNotAnchor = href.indexOf('#') === -1; | |
const isNotInternalLink = href.indexOf('YOUR_WEBSITE_URL') !== 0 && href.indexOf('/') !== 0; | |
if (isNotAnchor && isNotInternalLink) { | |
$(this).attr('target', '_blank'); | |
} | |
}); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment