Standard Compliant Way to Open A New Page Function

/ Published in: JavaScript
Save to your folder(s)

This function allows you to open a new window, without using any inline Javascript. Simply add rel="external" to the link you wish to open in a new window.

Copy this code and paste it in your HTML
  1. function doStandardsCompliantExternalLinks() {
  2. if (!document.getElementsByTagName) {return;}
  3. var anchors = document.getElementsByTagName("a");
  4. for (var i=0; i<anchors.length; i++) {
  5. if (anchors[i].getAttribute("href") && anchors[i].getAttribute("rel") == "external") {// for example: title="Download Adobe Reader" rel="external"
  6. anchors[i].target = "_blank";
  7. anchors[i].title += " - Opens in new window";
  8. }
  9. }
  10. }

Report this snippet


RSS Icon Subscribe to comments

You need to login to post a comment.