jqueryexample.com Open external links in new tab with jQuery - jqueryexample.com

Open external links in new tab with jQuery by jqueryuser

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 
Did you like this? Share it:

Discussion

No comments yet, be the first.

Leave a Comment