1

I'm fetching some html from the server which comes back as a string, but I need to insert a target='_blank' to all <a> tags within the html string. Is there a better or more efficient way to do this other than looping through the entire string to look for <a and then inserting target='_blank'?

0

5 Answers 5

2

There is the .replace method, using regex.

var string = "<a href='https://stackoverflow.com/'>Stack Overflow</a><br><a href='https://google.com/'>Google</a><br><a href='https://wikipedia.com'>Wikipedia</a>";

var newString = string.replace(/<a/g, "<a target='_blank'");

document.body.innerHTML = newString;

Sign up to request clarification or add additional context in comments.

3 Comments

What about the case where the a already has a target attribute? Like _self for example
You can use string.replace(/<a target='_[^]*'/g, "<a target='_blank'");.
You can use string.replace(/<a target='_[^]*'/g, "<a target='_blank'");.
1

First place the HTML into your page. Then immediately run code which modifies the DOM elements to include the needed attribute.

For example if you are placing the html into a container element with an id of container:

document.querySelectorAll('#container a').forEach(element => {
  element.setAttribute('target', '_blank')
})
<div id="container">
     <a href="http://google.com">Google</a>
     <a href="http://wikipedia.com">Wikipedia</a>
</div>

Comments

1
//if you want to do it in a string;
response.replace(/<a/g, '<a target="_blank" ');

//if object (html)
//first append it to DOM
let container = document.querySelector(".container");
container.innerHTML = response;
container.querySelectorAll("a").forEach(el => el.setAttribute('target', '_blank'));

Comments

1

You could create an html element with the html code that comes back as a string, then getElementsByTagName('a') and finally set the target attribute to be "_blank", for example:

let string = `<ul><li><a href="http://example.com/1">Link 1</a></li><li><a href="http://example.com/2">Link 2</a></li><li><a href="http://example.com/3">Link 3</a></li></ul>`;

var el = document.createElement("html");
el.innerHTML = string;
    
let anchors = el.getElementsByTagName("a");
for (let anchor of anchors) {
  anchor.setAttribute("target", "_blank");
}

console.log(el.innerHTML);

document.body.innerHTML = el.innerHTML;

Remember to use template literals ` to escape the double and single quotes of your html string.

Comments

0

Don't complicate htmlStrings more than they already are. Use this one liner after HTML has been rendered:

[...document.links].forEach(a => a.target = '_blank');

Demo

[...document.links].forEach(a => a.target = '_blank');

// Just to check if each link is correctly modified
console.log([...document.links].map(a => a.outerHTML));
a {
  font: 700 3vw/1.45 Verdana;
  display: block;
  width: max-content;
  margin-bottom: 8px;
  text-decoration: none
}

a:hover {
  text-decoration: underline;
}

.as-console-wrapper {
  width: 80%;
  min-height: 100%;
  margin-left: 20%;
}
<a href='example.com'>LINK</a>
<a href='example.com'>LINK</a>
<a href='example.com'>LINK</a>
<a href='example.com'>LINK</a>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.