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.

Recent Posts

13 Best WordPress Email Marketing Plugins

The article lists the 13 best WordPress email marketing plugins for 2022 - HubSpot, ConvertKit,…

6 hours ago

AI Chips in 2024: Navigating NVIDIA’s Dominance and Emerging Competitors

In 2024, NVIDIA remains a dominant force in the AI chip market with its innovative…

6 hours ago

21 Best ChatGPT Prompts for Marketing and How to Utilize Them Effectively

In the rapidly evolving realm of digital marketing, ChatGPT has emerged as an invaluable tool,…

18 hours ago

WP Cult attending WordCamp Denver 2009

It is official, today I purchased my ticket for WordCamp Denver! I as well as…

1 day ago

Navigating the AI Revolution: 7 Key Challenges for Web Professionals

AI's Transformative Impact on Web Professionals Artificial intelligence (AI) is revolutionizing the digital landscape, and…

2 days ago

Anfex.it/blog

2 days ago