Posted By

SamBenson on 03/16/10


link xhtml strict jquery anchor external

Versions (?)

Who likes this?

1 person have marked this snippet as a favorite


jQuery External Link Snippet for XHTML Strict 1.0

 / Published in: jQuery


There are plenty of jQuery snippets like this on Snipplr, but most of them are either overly complicated or defeat the object of not using the target attribute in your HTML. (Although to be honest, if you're 100% XHTML Strict and even vaguely Web Standards-minded you should let the user decide whether or not to open a link in a new window - we shouldn't force it upon them ).

This one however, IS simple - it looks for all anchor tags with rel="external" (the correct way to specify an external link) and binds a click event to them which, when activated, prevents the default action and opens the link in a new window.

Not a target attribute in site!

  1. <script type="text/javascript">
  2. $(document).ready(function(){
  3. $('a[rel="external"]').click(function(){ // When a link with rel="external"is clicked ...
  4. $(this).attr('href') ); // ... open a new window using the link's href attribute ...
  6. return false; // ... and prevent the default browser functionality (opening the link in the same window).
  7. })
  8. })
  9. </script>

Report this snippet  

You need to login to post a comment.