Picnic Website Code Tutorials

How To Open Links In a New Window With jQuery

4/02/2013

"jQuery Class External" Link "jQuery Rel External" Link

open new window As you probably know, target _blank is bad. So, using jQuery instead, here are two very simple ways to open links in a new window using either "class external" or "rel external". Neither write anything into the HTML. And both work identical - its your choice. Simply place jQuery and either of these two snippets at the bottom of your page, and place the rel or class in your links, and your good to go.

jQuery class="external"

$("a.external[href^='http://']").click(function(){window.open(this.href);return false;});
		

The html

<a class="external"></a>
		

jQuery rel="external"

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

The html

<a rel="external"></a>
		

Need help adding this or other code to your website? Post it here in the forum! Forum

Sponsors

Top Donators

Friends of Mine