Open external links in new tab with jQuery

selecting all anchor links on the page we can use below jquery code.

<script type="text/javacript">
$j(document).ready(function() {
//external attribute
$('a').each(function() {
   var a = new RegExp('/' + window.location.host + '/');
   if(!a.test(this.href)) {
  $(this).click(function(event) {
 event.preventDefault();
event.stopPropagation();
window.open(this.href, '_blank');
});    } });
    }
);
</script>
 Add CSS class to all external links 
Share this nice post:

One thought on “Open external links in new tab with jQuery

Leave a Reply

Your email address will not be published. Required fields are marked *