External Popup Links Using jQuery
Written by Cory S.N. LaViska on November 14th, 2007
How to make XHTML links that popup a new window and degrade gracefully using the jQuery JavaScript Library.
With the deprecation of the target attribute in XHTML Strict, opening links in new windows has become a bit trivial, if not annoying, to standardize. I always look for a consistent, unobtrusive approach that degrades gracefully; and since I use jQuery quite frequently, this is how I usually handle them.
The solution is a small piece of jQuery code in your $(document).ready() section:
$('A[rel="external"]').click( function() {
window.open( $(this).attr('href') );
return false;
});
});
Now, add rel="external" to all of the links that you want to open in a new window:
From here on out, users that have JavaScript enabled will receive external pages in new windows, while those without JavaScript will still be directed to the appropriate location.
I use rel="external" because it’s generally a good practice to limit popup links to external websites only. You could very well use rel="popup" instead, but I prefer the former for semantics.


Comments
#1 Martijn van der Ven on Nov 20th, 2007
#2 Cory S.N. LaViska on Nov 20th, 2007
#3 Dennis on Nov 21st, 2007
#4 eksith on Jan 28th, 2008
#5 Shir on Mar 11th, 2008
#6 Paul on Apr 10th, 2008
#7 Cory S.N. LaViska on Apr 10th, 2008
#8 Paul on Apr 11th, 2008
#9 Cory S.N. LaViska on Apr 26th, 2008
#10 Michael Sharman on Jun 1st, 2008
#11 asdf on Jun 4th, 2008
#12 Andrew Fox on Jun 11th, 2008
#13 Steven Hambleton on Jun 12th, 2008
#14 Shawn Dones on Jul 8th, 2008