BoldSource

Use JQuery to open external links in a new window

I spent some time searching the web for a solution and found a lot of out dated ones or ones that just weren’t what I wanted. Here is an easy to implement snippet for anyone interested:

  $("a[href^=http]").each(function(){
    if(this.href.indexOf(location.hostname) == -1) {
      $(this).attr('target', '_blank');
    }
  });

This is an updated version of the code found here: http://www.drupalcoder.com/story/80-automatically-open-all-external-links-in-a-new-window-using-jquery. Notice the @ symbol which is now no longer supported in Jquery 1.3.x.


  1. boldsource posted this
To Tumblr, Love Metalab