Javascript: automatically open external links in a new window
Caution
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’attribute “target='_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 attributes “onclick='window.open(this.href); 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://") anchor.target = "_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.
5 Comments
daniele · 29 November 2012 at 3:25 PM
It is too easy so…intendevo x Houses. I had to add the or
that:
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
ciaoooo
daniele · 29 November 2012 at 12:51 AM
This opens a new window for the prot only http and https?
TheJoe · 29 November 2012 at 11:31 AM
… they add a.
TheJoe · 21 September 2012 at 9:20 PM
Well, w3c specifications are even older (2002 – “http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”) 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: http://developer.yahoo.com/blogs/ydn/posts/2010/10/how-many-users-have-javascript-disabled/ 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 (http://www.sitepoint.com/standards-compliant-world), it is true that in 2012 it makes little sense to cite the case but.. if javascript is disabled?