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

Marie Osmond Net Worth: 5 Interesting Facts You Should Know

Marie Osmond Net Worth: 5 Interesting Facts You Should Know

Marie Osmond is an American actress, singer, and designer. She gained international recognition in the 1970s after her success in country music. Marie is also known for hosting the television variety show “Donny & Marie” with her brother Donny Osmond. Here are five interesting facts about Marie Osmond 1. Marie Osmond net worth is estimated […]

Blueface Net Worth: 5 Interesting Facts About the Rapper

Blueface Net Worth: 5 Interesting Facts About the Rapper

Blueface Bleedem, or simply Blueface, is an American rapper from Los Angeles currently signed to Birdman’s Cash Money West label. He rose to fame after releasing single ‘Deadlocs’ on music platform SoundCloud. The song has received more than 2.5 million streams. Here are some of the interesting facts about Blueface. 1. Blueface net worth is […]

FaZe Tfue Net Worth: 5 Interesting Facts You Should Know

FaZe Tfue Net Worth: 5 Interesting Facts You Should Know

Turner Tenney, professionally known as FaZe Tfue or Tfue is an American gamer, Twitch streamer and a member of FaZe Clan. Some people considered him the better player than Ninja and also ‘The Best Player in the World’. Here are some of the interesting facts about Faze Tfue including his path to success, net worth, […]