Wednesday, August 1, 2012

How to open all anchors in new window/tab using javascript

In this post i will given a script to open all anchors in new tab when we click on anchors links.I have tried so many ways to do this task .Finally the below script will given the result as i need.In this script i get the anchors tags first then assign the target property to blank.
function OpenLinksInNTab()
{
  if (!document.getElementsByTagName)
  return;
  var getAllanchors = document.getElementsByTagName("a");
  for (var j=0; j<getAllanchors.length; j++)
  {
      var anchoritem = anchors[j];
      if(anchoritem.getAttribute("href"))
   anchoritem.target = "_blank";
  }
}
window.onload = OpenLinksInNTab;
Post a Comment

Bel