This post written by Austin

A web designer and avid WordPress guru. Visit my personal blog, and please follow @WPCult on twitter for site updates. You can also follow me personally @TheFrosty.

  • Share/Bookmark

6 responses to “How to: Open external links in a new window”

  1. Shawn Parker

    Good tip, thanks for picking this up after my blunder. A couple more things to note:

    1. Instead of worrying about jQuery’s noConflict you can simply use jQuery(…) instead of $ – then everything you do is safe even if jQuery doesn’t get started in no conflict mode.

    2. jQuery(document).ready(function() { can be replaced with just jQuery(function() {

    3. To be xhtml future proof you can’t use target=”_blank” any more. Something like this would be more appropriate:

    jQuery(function() {
    jQuery(“a:not([@href*=http://YOURSITE.com/])”).not(“[href^=#]“)
    .addClass(“external”)
    .click(function(){
    window.open(this.href); // pop a new window
    return false; // return false to keep the actual link click from actuating
    })
    });

  2. Open Links in New Windows or Tabs Without Target=”Blank”

    [...] recently came accross an interesting post on WP Cult that discussed opening external links in a new window with jQuery. I adapted the jQuery snippet a little and came up with [...]

  3. Elena Coen

    Hi there,

    Is there a hack for this that would allow links from a folder to open automatically in a new window? I have an RSS feed from another install I did in a folder. I want anything from there to open in a new window but because it starts with the same URL, such code recognizes it as a match and keeps it opening in the same window. So http://www.mysite.com/anything else would open in same window, any external URLS plus http://www.mysite.com/jobs I want to open in a new window.

    Thank you, Elena

  4. Udegbunam Chukwudi

    Wonderful tutorial. Would definitely come in handy for some folks. For me, when I link to old posts on my blog from a new post, I like them opening in a new window so the reader doesn’t get lost trying to find his/her way back to the start.
    BTW is there a way of making this code force all links to open in a new window?
    .-= Udegbunam Chukwudi´s last blog ..Top 10 Ways Nigerians Make Money Online =-.

Shopping Cart

Your theme cart is empty

External Link Preferences

Feedburner & Twitter Stats

Tag Cloud

  • WordPress Premium Themes

    • Agent Theme
    • Black Canvas
    • Busy Bee
    • CelebrityPress
    • Chrome Theme
    • PaperCut
    • Revolution 2
    • WP Realtor