Using external for rel attribute on a link, you can specify that the link is outside of the current site. This attribute is commonly used for SEO and styling options. And to open a link in a new window when user clicks it you can use target attribute. What if you wanted all external links to open in a new window? jQuery is the answer.

To easily add target blank attribute to all external links you can use attribute selectors that come with jQuery:

[js title=”false”] jQuery(function() { jQuery(‘a[rel~=external]’).attr(‘target’, ‘_blank’); }); [/js]

This simple line of code adds target="_blank" attribute to all external links on your site. However, this should be done when the DOM is loaded. So we need to wrap this code with the document ready function like this:

[js]<script type="text/javascript">
/* <![CDATA[ */
jQuery(document).ready(function() {
//External links
jQuery(function() { jQuery(‘a[rel~=external]’).attr(‘target’, ‘_blank’); });
});
/* ]]> */
</script>[/js]

If you want this trick for your wordpress site, put this code in footer.php in your theme just before </body> tag. This way all your rel="external" links will open in a new window.

Hope you like this simple trick.

Deal Notice: We are using A2 hosting for all of our websites, and we are really happy with their service. So we wanted to share you this discount link for you to try it out. If you get a hosting using this link, you will also help us build better content for you: SSD Hosting - 300% Faster Than Standard Hard Drives