Back jQuery

Open link in a new window using jQuery

WRITTEN BY ON 09 Jan 2011
17,279 VIEWS • SHARES
4 comments

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>
Join the discussion

Comments will be moderated and rel="nofollow" will be added to all links. You can wrap your coding with [code][/code] to make use of built-in syntax highlighter.

4 comments
James 14 years ago
The same jQuery function could be used to add a class which appended the standard 'new window' icon to the link.

Adding this class through jQuery has the benefit of the icon appearing only when javascript is enabled (and clicking will actually result in a new browser window)
Reply
sadirdin 13 years ago
your code will not work my browser ,my browse is internet exlorer 8
Reply
Kevin Liew 13 years ago
Try the other way around:

$('a[rel=external]').click(function(){
$(this).attr('target', '_blank');
});
Reply
Sharp Coders 12 years ago
you can try this one..

$(document).ready(function(){
$('#link_id').click(function(){
window.open('url', 'name', 'features', 'replace');
return false;
});
});

Source: http://sharp-coders.blogspot.com/2012/09/how-to-open-link-in-new-window-using.html
Reply