Connect with us

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 $

 

7 Comments

7 Comments

  1. Pingback: Open Links in New Windows or Tabs Without Target=”Blank”

  2. Pingback: darrinb.com | opening external links in a new window using jquery

You must be logged in to post a comment Login

Random Search Terms

Title

Top Articles

powered by WassUp

Recent Posts: Fully Net Worth . com

Alex Scott Net Worth: How Rich is Alex Scott Actually?

Alex Scott Net Worth: How Rich is Alex Scott Actually?

Alex Scott is a former English footballer who is best known for her three stints at Arsenal as a right back. Moreover, she is the second most capped player in the history of the Women’s English national team with a total of 140 appearances for them. As of 2019, Alex Scott net worth is estimated […]

Reanne Evans Net Worth: How Rich is the English Snooker Player?

Reanne Evans Net Worth: How Rich is the English Snooker Player?

Reanne Evans is an English snooker player and the current World Women’s Snooker Champion. She has held the title a record 12 times, with 10 consecutive titles coming between 2005 and 2014. Evans is currently ranked number one on the World Women’s snooker Tour. As of 2019, Reanne Evans net worth is estimated to be £500,000. Evans was […]

Sasha Velour Net Worth: How Rich is the Drag Queen Actually?

Sasha Velour Net Worth: How Rich is the Drag Queen Actually?

Alexander Hedges Steinberg also known by the stage name Sasha Velour is an American drag queen and artist. Velour is best known for winning the 9th season of RuPaul’s Drag Race. As of 2019, Sasha Velour net worth is estimated to be $2.5 million. Velour was born on June 25, 1987, in Berkeley, California as the only child of Mark […]