Skip to content

Instantly share code, notes, and snippets.

@mlent
Last active September 28, 2022 09:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mlent/60010ba2bb46c5bcad8af114d283c939 to your computer and use it in GitHub Desktop.
Save mlent/60010ba2bb46c5bcad8af114d283c939 to your computer and use it in GitHub Desktop.
open-links-in-new-tab.js
<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