0

When I was writing (a lot of) <a> tags I didn't write 'target="_blank"', so none of the links are leading to another windows or tag.

Is there a way to add "target='_blank'" to all the links with JavaScript?

chrki
  • 6,143
  • 6
  • 35
  • 55
Breno
  • 101
  • 2

2 Answers2

4

You don't need JavaScript at all. You can use the base element in your head to specify a base URL or target for anchors.

<base target="_blank"> will make all links on your page open in new windows and/or tabs.

More information on the base element can be found on MDN.

Garbee
  • 10,581
  • 5
  • 38
  • 41
  • Thank you!!! It worked. It's because I can't study on a paid place, so what is free I am studying, but it isn't working. – Breno Feb 11 '16 at 19:51
1

Previously answered here: How do I add target="_blank" to a link within a specified div?

Code:

/* here are two different ways to do this */
//using jquery:
$(document).ready(function(){
  $('#link_other a').attr('target', '_blank');
});

// not using jquery
window.onload = function(){
  var anchors = document.getElementById('link_other').getElementsByTagName('a');
  for (var i=0; i<anchors.length; i++){
    anchors[i].setAttribute('target', '_blank');
  }
}
// jquery is prettier. :-)
Community
  • 1
  • 1
ketchupisred
  • 661
  • 3
  • 16