W
W
wiyod2018-02-27 12:40:40
JavaScript
wiyod, 2018-02-27 12:40:40

Is it possible to automatically make target blank for external links?

Actually a question. Is it possible to have automatic behavior for ALL external links target blank, preferably by means of HTML? It seems there was a similar tag in the head that specifies this behavior? If you can’t use HTML, then what would you advise on JS (this is probably more difficult, because Vue is used, and even the content comes via the API as JSON and is rendered via v-html)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Pavel, 2018-02-27
@wiyod

I haven't heard that about html.
Jquery

$(document).ready(function(){
  $('#content a').attr('target', '_blank');
});

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

+ Checking for a domain and adding:
$('#content a').each(function() {
   var linky = new RegExp('/' + window.location.host + '/');
   if (!linky.test(this.href)) {
      $(this).attr("target","_blank");
   }
});

I
Ivan Bogatov, 2018-02-27
@Malekith

Isn't that what you're looking for?
https://www.w3schools.com/tags/tag_base.asp
htmlbook.ru/html/base

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question