How to manage all link tags in html from one place?

I am changing my code for my site. I want all links to open in a new window. However, instead of adding:

target="_blank" 

for all links, I was wondering if I could somehow control all link tags from one place.

+4
source share
2 answers

You can try adding the base tag to the html header

 <base target="_blank"> 

http://jsbin.com/ezeyij/1/edit

Note. It also affects the purpose of the forms.

+6
source

You really need to give us more information. Do you use CMS? Is this a website written from scratch?

You can set all the links on the page to open them in a new tab with a bit of javascript. Assuming your page has jQuery, you can do:

 $(function() { $('a').prop('target', '_blank'); }); 

Demo: http://jsfiddle.net/9p4P2/

Or, if you are not using jQuery, you can use your own JavaScript:

 var anchors = document.getElementsByTagName('a'); for (var i = 0; i < anchors.length; i++) { anchors[i].setAttribute('target', '_blank'); } 

EDIT:

If you want only links related to other websites to open in new windows, you could:

 $(function() { $("a[href^='http://']").prop('target', '_blank'); }); 
+2
source

Source: https://habr.com/ru/post/1480293/


All Articles