Open external links in a new window using JQuery

jQuery - Write Less Do More

jQuery – Write Less Do More

Often times on sites it’s useful to automatically have any external link open in a new window. This is a very simple solution that just overrides any link that has the attribute ‘rel’ with the value of ‘external’ and forces them to open in a new window. There are obviously other ways to accomplish this but this is a very simple method that also validates in XHTML 1.0 strict.

However, we do use HTML5 openly now, so why try to maintain high standards?

It’s all about your code quality. If that is good, then no matter the DOCTYPE, your code stands a great chance of validating every time with very little amends.

$('A[rel="external"]').click( function() { $(this).attr('href')
  return false;

The method about consists of a simple jQuery function and forces external links to open in a new window so make sure you’ve referenced the JQuery Library framework BEFORE you call any JQuery custom scripts. This is useful for any site with unpredictable content when you know you want external links to open in a new window.

Happy coding.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.