I have a HTML page, with several links on it. I want to make all those links open in a different tab, so I tried using
var foo = document.getElementsByTagName("a");
for(var i = 0; i < foo.length; i++) {
foo[i].target = "_blank";
}
But why is that not working (as far as I know, getElementsByTagName() returns an array, I might as well be wrong)?
Try to use
setAttributeand put your script inside ofwindow.onloadhandler(this is for ensure,thatDOM Hierarchyis completely constructed and you can accessDOM Elements):Actually
getElementsByTagNamereturnsNodeListobject,which has some similar functionalities withArray,but it’s not anArray.