BoldSource - Bits and Beats from Blake Lucchesi

  • Random
  • Archive
  • RSS

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.

    • #jquery
    • #webdev
  • 2 years ago
  • 1
  • Permalink
  • Share
    Tweet

Portrait/Logo

About

I'm Blake Lucchesi and I'm the founder and lead developer of ChapterBoard, a web app for fraternities and sororities.

Me, Elsewhere

  • @blakelucchesi on Twitter
  • BlakeLucchesi on Last.fm
  • Linkedin Profile
  • blakelucchesi on github

Twitter

loading tweets…

  • RSS
  • Random
  • Archive
  • Mobile

Effector Theme by Carlo Franco.

Powered by Tumblr