Change <a> to target="_blank" in <a> click event - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Anchor

Description

Change <a> to target="_blank" in <a> click event

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript">
    window.onload=function(){/*from  ww w. ja v  a 2 s.  c  o  m*/
document.addEventListener("click", function (e) {
    if (e.target.tagName == "A" && !e.target.hasAttribute("target")) {
        e.target.setAttribute("target", "_blank");
    }
});
    }

      </script> 
   </head> 
   <body> 
      <a href="http://java2s.com">test</a>  
   </body>
</html>

Related Tutorials