Categories: Tips & Tricks

How to: Open external links in a new window

Over the weekend, I attended WordCamp Denver, and I was asked by John Hawkins how to force links to open in a new tab with out editing the source code. So, today lets learn a simple jQuery trick to open all external links in your site in a new tab or window. We are going to make sure you have jQuery active on your site, you can do this easily in WordPress, since it’s bundled with the latest installations. Use this code in your header: <?php wp_enqueue_script('jquery'); ?> then, below the wp_head add the following:

<script type="text/javacript">
var $j = jQuery.noConflict();

$j(document).ready(function() {
//external attribute
    $j("a:not([@href*=http://YOURSITE.com/])").not("[href^=#]")
        .addClass("external")
        .attr({ target: "_blank" });
    }
);
</script>

That’s it, just make sure you change the http://YOURSITE.com to your website.

Update

If you like you can remove the var $j = and replace all $j with simply just $

 

Web Master

Hi, I am Miguel, I bought this site in 2009. So I now run or manage the site. Please visit my new website or follow me on twitter @W3i.

View Comments

  • 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
    })
    });

  • 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

    • Hmmm.. This may be possible with some anchor not attributes in jQuery. Let me browse around and see what I can come up with..

  • 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 =-.

Recent Posts

How to Incorporate a Website into Your Website Marketing Efforts

In today's techno-centric culture, you simply must have a business website, no matter how small…

5 hours ago

WordPress Tips for Photographers

Photography is the art of capturing naturalistic images with the help of photographic cameras. It…

17 hours ago

10 Best Digital Marketing Plugins for WordPress 2022

Have you opened up a new website or a business and want to drive more…

1 day ago

Configure your WordPress Site for SEO

WordPress has evolved greatly from its origin as a powerful blogging platform to an effective…

2 days ago

In the yеar 2002, Mіcrоѕoft саmе uр wіth a whоlе new рlаtfоrm for developing арplicаtiоnѕ.

Busіnеsѕеs аnd orgаnіzatіоns аll аrоund the wоrld arе loоking fоr а ѕоlutiоn thаt wоuld prоvе…

2 days ago

The following 5 factors detail the key techniques to influence and improve your ranking on Google.

It is essential to draw people’s attention to your website or blog. It isn’t easy…

3 days ago