TheJoe.it Into the (open) source

17Sep/125

Javascript: automatically open external links in a new window

javascript

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 bit 'dated, but still valid if declared tag in the "HTML" Site.

The web designer has different 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 inclusion of 'attribute "target='_blank'" inside the tag to refer to the links "<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 that deals with a certain rigidity to establish standards that webmasters and developers of browsers must follow to create and view these pages "XHTML", thus ensuring maximum compatibility between the site and the various browser. Read more about the aims of the organization are explained (as well as on organization's website) to the relevant page of Wikipedia.

About

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.

Comments (5) Trackbacks (0)
  1. 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

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

  3. 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).

  4. 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?


Leave a comment

No trackbacks yet.