Open All External Links in New Tabs

August 4th, 2010

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>

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>