Javascript: automatically open external links in a new window

Published by TheJoe on

Estimated reading time: < 1 minute


This article was published more than a year ago, there may have been developments.
Please take this into account.

Today's article is a little advice on the links to have a website conforms to w3c(1). This procedure is valid in the case we are using version “XHTML 1.0 Strict“, a powder’ dated, but still valid if declared tag in the “HTML” Site.

The web designer has several ways to set the aperture of a given link in a new page (or new tab) Part browser. The first to be implemented has been the insertion of’attributetarget='_blank'” within the reference tag to link “<a>“. This is the easiest way to open a link in a second window, but unfortunately does not respond to standard w3c. The solution suggested by the W3C is the inclusion of these other attributesonclick='; return false;'“, actually solving the problem with some brief instruction javascript.

Fortunately for us JavaScript is a language very different, one of the behaviors that we can manage is the opening of the link. With this third and final example will open in a new tab all external links, namely those that as a reference starting with “http://” saving us from having to write every time the code suggested by the w3c on any external link. The reference number is this:

function externalLinks() {
  if (!document.getElementsByTagName) return;
  var anchors = document.getElementsByTagName("a");
  for (var i = anchors.length-1; i>=0; i--) {
    var anchors ANCHOR;[i];
    if (anchor.href && anchor.href.substr(0,7) == "http://") = "_blank";
window.onload = externalLinks;

(1)_ The W3C is the non-profit organization dedicated to establishing a certain rigidity standards that webmasters and browser developers must follow to create and view these pages “XHTML“, thus ensuring maximum compatibility between the site and the various browser. Read more about the organization's intentions are explained (as well as on site organization) to the relevant page of Wikipedia.


I keep this blog as a hobby by 2009. I am passionate about graphic, technology, software Open Source. Among my articles will be easy to find music, and some personal thoughts, but I prefer the direct line of the blog mainly to technology. For more information contact me.


daniele · 29 November 2012 at 3:25 PM

It is too easy so…intendevo x Houses. I had to add the or
if ((anchor.href && anchor.href.substr(0,7) == “http://”) || (anchor.href && anchor.href.substr(0,8) == “https://”))

extending the range to 8 and adding an s

daniele · 29 November 2012 at 12:51 AM

This opens a new window for the prot only http and https?

TheJoe · 21 September 2012 at 9:20 PM

Well, w3c specifications are even older (2002 – “”) and are still used as a standard. And it is also true that the HTML 5 is out for a while ', but it is still the standard.

The only way to ensure compatibility with the forced opening of a link in a second tab is to use the “transitional” in place of “strict”. The transitional would be interim.

In this regard we point out an interesting statistic compiled by Yahoo: which has only a few countries in the world (this is the most updated: 2010). Even if the percentage is low (in the worst case around 2%) you teach me that you need to ensure compatibility.
In fact, the method described in this article does not translate into a lack of compatibility for the user who has js disabled, simply links will open in the same page (the webmaster will not have achieved its goal, but the user will not be damaged by this).

Luca · 18 September 2012 at 10:41 PM

Apart from that the original article 2003 is a bit’ older (, it is true that in 2012 it makes little sense to cite the case but.. if javascript is disabled?

Leave a Reply

Avatar placeholder

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.