»
EnglishFrenchVietnamese

Print - Using the Target Attribute with JavaScript - JavaScriptBank.com

Full version: jsB@nk » Snippet » Using the Target Attribute with JavaScript
URL: http://www.javascriptbank.com/using-the-target-attribute-with-javascript.html

Using the Target Attribute with JavaScript © JavaScriptBank.comSometimes you may need to open certain links in a new window. The 'target' attribute is deprecated and is therefore not allowed when using XHTML with a STRICT. But when use this JavaScript, you will be able to keep your pages valid while also using the target attribute to open new windows.

Full version: jsB@nk » Snippet » Using the Target Attribute with JavaScript
URL: http://www.javascriptbank.com/using-the-target-attribute-with-javascript.html



JavaScript
<script type="text/javascript">// Created by: Alan Coleman | http://www.alancoleman.co.uk function externalLinks() {  if (!document.getElementsByTagName) return;  var anchors = document.getElementsByTagName("a");  for (var i=0; i<anchors.length; i++) {    var anchor = anchors[i];    if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") {      anchor.target = "_blank";    }  }}// Multiple onload function created by: Simon Willison// http://simon.incutio.com/archive/2004/05/26/addLoadEventfunction addLoadEvent(func) {  var oldonload = window.onload;  if (typeof window.onload != 'function') {    window.onload = func;  } else {    window.onload = function() {      if (oldonload) {        oldonload();      }      func();    }  }}addLoadEvent(function() {  externalLinks();});</script>


HTML
<ul>  <li><a href="http://www.javascriptbank.com/" rel="external">Web Developer</a></li>  <li><a href="http://javascriptbank.com/" rel="external">JavaScript Source</a></li>  <li><a href="http://www.javascriptbank.com/" rel="external">WebReference.com</a></li></ul>