<p>In a recent project at work, I had completed implementing a site, and the client was reviewing it. Their company decided that they wanted all external links to open in a new window. This is not a practice that I really like, and others agree:</p>

<blockquote>
<p>Opening up new browser windows is like a vacuum cleaner sales person who starts a visit by emptying an ash tray on the customer's carpet. Don't pollute my screen with any more windows, thanks (particularly since current operating systems have miserable window management). If I want a new window, I will open it myself!</p>
<p><cite>Jakob Nielsen: <a href="http://www.useit.com/alertbox/990530.html">The Top Ten Web Design Mistakes of 1999</a></cite></p>
</blockquote>

<p>Breaking the back button is never a good thing. But this article is not about that.</p>

<p>Back to the site I was implementing. I was really not interested in adding <code>target="_blank"</code> to every single external link, so I came up with a better solution…</p>

<h2 class="replaceHeadline">The Solution</h2>
<p>The site was already using jQuery for other functions, so I figured that I should just use some of its powerfulness to make my life easier.</p>

<p>With a couple lines of code, I solved my problem:</p>

<div class="codeblock"><code>
$(document).ready(function(){<br /> $("a[@href^='http']").attr('target','_blank');<br />}); 

</code></div>

<p><a href="//trevor-davis.com/play/jquery-open-external-links-in-new-window/">Check out the demo to see the results.</a></p>

<h3 class="replaceHeadline">Explanation</h3>
<p>If you know anything about <a href="//trevor-davis.com/blog/tutorial/css-attribute-selectors-explained/">attribute selectors</a>, that should look familiar to you. Basically, it is saying, apply the attribute <code>target="_blank"</code> to all links that begin with <strong>http</strong>.</p>

<p>Really easy, really powerful, problem solved. Some may say it almost might be preferable to browse the site with JavaScript disabled so that the external links would just open in the same window. But that debate is for another time.</p>