Categories
JavaScript Answers

How to add meta tag in JavaScript?

Spread the love

Sometimes, we want to add meta tag in JavaScript.

In this article, we’ll look at how to add meta tag in JavaScript.

How to add meta tag in JavaScript?

To add meta tag in JavaScript, we call createElement.

For instance, we write

const meta = document.createElement("meta");
meta.httpEquiv = "X-UA-Compatible";
meta.content = "IE=edge";
document.head.appendChild(meta);

to call createElement to create a meta element.

Then we set the http-equiv attribute with

meta.httpEquiv = "X-UA-Compatible";

We set the content attribute with

meta.content = "IE=edge";

And we append it as the last child of the head element with

document.head.appendChild(meta);

Conclusion

To add meta tag in JavaScript, we call createElement.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *