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

Top Searches

powered by WassUp

Recent Posts: Fully Net Worth . com

Bikram Choudhury Net Worth: How Rich is the Yoga Teacher?

Bikram Choudhury Net Worth: How Rich is the Yoga Teacher?

Bikram Choudhury is an Indian-American yoga instructor best known as the founder of Bikram Yoga, a form of hot yoga performed in a series of 26 hath a yoga postures in a hot environment. As of 2019, Bikram Choudhury net worth is estimated to be $75 million. Choudhury was born on February 10, 1944, in […]

Matt Higgins Net Worth: 5 Interesting Facts You Should Know

Matt Higgins Net Worth: 5 Interesting Facts You Should Know

Matt Higgins is a celebrated man, who came from the downsides of life to the upper echelons of the world. An investor, businessman, innovator, top executive, and business advisor. Although he had a hard early life at the beginning, he never let background shackles deter him as he steadily moved his way to success. He’s […]

Luh Kel Net Worth: How Rich is the Rapper Actually?

Luh Kel Net Worth: How Rich is the Rapper Actually?

Turran Coleman, who is better known as Luh Kel, is an American singer and songwriter. He is best known for his hit single called “Wrong” which managed to peak at number 37 in the Billboard Hot R&B / Hip-Hop Songs chart and was certified Gold by RIAA in 2019. As of 2019, Luh Kel net […]