I would like to make a JavaScript that would put in each link the attribute target="_blank", so that the link would open in a new tab. This is how I did it:
function open_links_in_new_tabs() {
var links = document.documentElement.getElementsByTagName( "a" );
for(var link in links) {
link.setAttribute("target", "_blank");
}
}
window.onload = function() { open_links_in_new_tabs(); }
But, this doesn’t work. Do you see where the mistake is?
Thanks,
Ivan
The
foo in barsyntax doesn’t work onNodeListobjects (a.k.a. “the stuff returned bydocument.getElementsByTagName“).Use a plain old
for (var i = 0; i < links.length; i++)(withlinks[i]instead oflink, of course) loop and it should work.