Tuesday, December 22, 2009

Easy way to make external links to open in a new tab/window

A few months ago I meant to update all my external links to open in a new tab. I never got around to it, but today I found a post by Mads that reminded me. His post is about how to embed an image in a css file.

I'm sure there are better ways to do this, but this was easy and the first thing that came to mind.

I had been meaning to add jQuery to the site. The first line goes and gets the latest jquery 1.* version from google. The second line uses a css selector to add target=blank to all the hrefs that don't point to my site.
<script src='http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js' type='text/javascript'></script> 
<script type='text/javascript'>
$("a[href^=http://]").not("a[href^=http://www.yourdomain]").attr("target", "blank");
</script>

0 comments:

Post a Comment