Skip to Content

Use jQuery to Replace target=”_blank”

jquery-logo

This is just one reason more people need to embrace jQuery.

Here is an easy way to replace target="_blank" in external links on your site. jQuery makes this a cinch! This is required if you choose to write strict XHTML.

I'm personally starting to clean much of my site up, this one of those easy fixes that is very easy to implement. ;)

Make certain this all goes in the head of your page or template...

This first part calls the jQuery resources on Google's servers THX Google! - note you don't have to do this part if you have the jQuery files hosted locally :

http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"

This second part is the nitty gritty:

$(function() {
$('a[rel*=external]').click( function() {
window.open(this.href);
return false;
});
});

The last thing you will need to do is... tag the links you want to open in a new window with:

rel="external"

No Responses to “Use jQuery to Replace target=”_blank”” Leave a reply ›

Leave a Reply

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>