Jepson Center for the Arts

Opening External Links in a New Window Using jQuery

…continued from previous page.

Initially I got the idea for this script from this script on Liviu Holhos’ blog. The script explains a way to add an image next to all external links on a site using the external site’s favicon. I figured if there were a way to add a favicon, there must be a way to automatically add the target="_blank" attribute to all external links. In my research, I discovered the 3 previously mentioned scripts but discovered the issues with each, so I decided to write a script from scratch.

The Code

First let’s get the script in here so we can see what we’re working with.

$(document).ready(function() {
    $('a[href^="http://"]').filter(function(){
    return this.hostname && this.hostname !== location.hostname; })
        .addClass("external")
        .attr({ title: "Link will open in new window" })
        .click( function() {
            window.open(this.href);
            return false;
        });
});

Now let’s break the code down and see what’s going on line by line.

In the first line, we’re telling jQuery not to load the script until the DOM is loaded, but before any page content is loaded:

$(document).ready(function() {
    // the rest of the script goes in here
});

In the second line, we’re telling the script to find all links that begin with “http://”…

$('a[href^="http://"]').filter(function(){

…and filter out links that have the same hostname as the current page:

return this.hostname && this.hostname !== location.hostname; })

On the remaining links we’re going to add a CSS class of “external” so we can style all external links. (If we want to.):

.addClass("external")

Now we’re going to let the visitor know that the link he/she is about to click on will open in a new window. We do this by adding a title attribute with a value of “Link will open in new window”. This will display as the tooltip when the visitor hovers over this link:

.attr({ title: "Link will open in new window" })

And in the last section, we’re going to tell the browser to open a new window when the link is clicked. By using (”window.open” instead of target="_blank", we’re keeping our links standards‐compliant):

.click( function() {
    window.open(this.href);
    return false;
});

Non‐WP Installation

To use this script on your site (non‐WP), you must first have the jQuery library installed on your web server or call it from a third‐party such as Google. You can get the latest copy here from the official jQuery website.

Once you’ve installed jQuery on your server, place a call to it in your site’s <head> section like so:

<script type="text/javascript" src="/js/jquery-1.2.6.min.js"></script>

Then copy/paste the external link script above in a new document using your favorite text editor and save it as “jquery_externalLink.js” (or whatever name you want). Then upload it to your web server and call it in the <head> section of your site like you did with the jQuery library above.

And that’s it! Now all external links will open in a new window, but any link that is internal will open in the same window.

Continue reading to learn how to install this in a WordPress installation.

Continues…

Snapshot: Post

Posted: July 5th, 2009

In this article we'll discuss working with external links and the semantically correct, user‐friendly, developer‐friendly way to get them to open in a new window or tab using the jQuery library and a small jQuery script. Additionally, we'll review how to implement this in a WordPress installation.

Disclaimer: While this script may come in handy for others, I designed it specifically for use on this site. I haven’t tested it in any version of Wordpress other than those that I’ve used (v2.6.3–v2.8.6). Feel free to use it if you’d like, but I make no guarantee it will work with your current theme or Wordpress setup. For a complete Copyright Notice and Disclaimer, visit the About Section of this site.

Topics

Or you can check out the full archive here: Article Archive.

Search