DOWNLOAD DEMO
Current Version: 2.4.0.3 2014-04-14
Need a hand? Join the community.

Links Should Not Open In a New Window

The W3c advocates not opening links in new windows, in fact, in Xhtml and in Html 5 the target attribute is not valid markup (update: the latest html 5 spec does in fact allow the target attribute but it is mainly considered useful for iframes).

The W3C logic is based on the idea that the user should be in control at all times and browsers all give the user the ability to open links in a new window or tab and forcing that upon them is generally confusing and takes control away from the user.

Unfortunately, opening in a new window has become such a widely used technique that often users now expect it and some argue its confusing the user if an external link doesn't open in a new window. Myself I side with the W3C on this, its usability 101 to keep the user in control at all times but I don't expect everyone to agree with me or the W3C. I would strongly advise any of my clients to not open links in new windows, I would tell them it is very 2003 and Web 1.0 thinking. But if the client insists what can you do, in the end you must satisfy them.

If all web site owners would follow the standards and advice of the W3c and if all the users would learn at least the basics of using their web browsers, the web would be a better place for everyone. So to the best of my ability I try to advocate for not opening links in new windows and also advocate to teach users how to use their browsers. All browsers provide a way to easily open links in a new window, generally right clicking provides a context menu to open a link in a new window or tab, and wheel clicking with the mouse also opens a link in a new tab. Mobile browsers do it by pressing and holding down a link. We should empower all of our users with this knowledge so they can be in control and have a better experience on the web. For users who do know how to open links in new windows when they want to, it is very annoying when a site forces a new window on them. Admittedly I have encountered users who get annoyed when links don't open in a new window, but this is generally because they have become used to this behavior from the bad practices of sites they visit and they haven't figured out how easy it is to open in a new window by choice.

But I Need/Want To Do It Anyway

If you've read this far and still want to open links in a new window, here is a little javascript snippet that will automatically open all external links in a new window, if a customer insists I would use this.

Note that using the target attribute will make your markup invlaid. Of course browsers are very forgiving of invalid markup in general so it isn't that big a problem but you should be aware of it and also make your customer aware of the tradeoff.

<script type="text/javascript">
$(document).ready(function() {
  $("a[@href^=http]").each(
    function(){
     if(this.href.indexOf(location.hostname) == -1) {
        $(this).attr('target', '_blank');
      }
    }
  )
});
</script>

 

References

Last Updated 2011-06-14 by Joe Audette