< Back to Guides

How to automatically open all external links in a new tab

When a visitor is browsing your website, clicking on an external link can disrupt their workflow by taking them away from your site onto the referred one. It’s a common issue often leads to a somewhat annoying cycle of clicking back and forth.

By applying the following jQuery snippet to your site you can automatically resolve this issue immediately.

This technique is valuable for anyone seeking to enhance their web navigation, from casual users to web developers.

<script>
  (function( $ ) {
    'use strict';
    $(function() {
      $("a").each(function() {
        var a = new RegExp('/' + window.location.host + '/');
        if (!a.test(this.href)) {
          $(this).attr("target","_blank");
        }
      }})
    });
  })( jQuery );
</script>