Open All External Links in New Tabs

August 4th, 2010
By: Cory Mathews

Here is a quick bit of jQuery to make all external links have the target=_blank attribute.

<script type="text/javascript">
$(function(){
$("a[href^='http:']").not("[href*=EXAMPLE.com']").attr({ target: "_blank" });
});
</script>

You could also use this same technique to add a small friendly image next to these links  notifying the user they are about to leave the site.

<script type="text/javascript">
$(function(){
$("a[href^='http:']").not("[href*=EXAMPLE.com']").attr({ target: "_blank" }).append(" <img src=\"outLink.jpg\">");
});
</script>