Posted By

chippper on 02/04/09


Tagged

javascript window standards external


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

luman


Open Link in an External Window via Javascript


 / Published in: JavaScript
 

URL: http://www.sitepoint.com/article/standards-compliant-world/3/

Rather than using target="_blank", which is deprecated by the W3C in XHTML, you should use rel="external" for links that you want to pop out in a new window. This is the javascript that makes it happen.

  1. function externalLinks() {
  2. if (!document.getElementsByTagName) return;
  3. var anchors = document.getElementsByTagName("a");
  4. for (var i=0; i<anchors.length; i++) {
  5. var anchor = anchors[i];
  6. if (anchor.getAttribute("href") &&
  7. anchor.getAttribute("rel") == "external")
  8. anchor.target = "_blank";
  9. }
  10. }
  11. window.onload = externalLinks;

Report this snippet  

You need to login to post a comment.