Open link in a new window using jQuery

Well, "target" attribute in anchor tag is not a valid attribute in strict doc type. Here is the best solution to open web pages in new window .

JS - jQuery

When you click on the link, it detects for rel attribute, if the value of rel attribute equal to "external" it will set the target to _blank, therefore, it opens in new tab/window :)

$('a[@rel$='external']').click(function(){
  this.target = "_blank";
});

HTML/XHTML

This is how you use it.

<a href="http://www.queness.com" rel="external">Queness.com</a>

Share the love

Leave a Comment

Please keep in mind that comments are moderated and rel="nofollow" is in use. You can use [code][/code] if you want to write codes. Don't spam us :) Thanks!

Advertisement