Categories
JavaScript Answers

How to use JavaScript to change the meta tags of the page?

Spread the love

Sometimes, we want to use JavaScript to change the meta tags of the page.

In this article, we’ll look at how to use JavaScript to change the meta tags of the page.

How to use JavaScript to change the meta tags of the page?

To use JavaScript to change the meta tags of the page, we can set the properties that are available in a meta element.

For instance, we write

document.getElementsByTagName("meta")["keywords"].content =
  "My new page keywords!!";
document.getElementsByTagName("meta")["description"].content =
  "My new page description!!";

to select the meta tag with the name attribute set to keywords with

document.getElementsByTagName("meta")["keywords"]

and set its content attribute value.

Likewise, we select the meta tag with the name attribute description with

document.getElementsByTagName("meta")["description"]

and set its content attribute value.

Conclusion

To use JavaScript to change the meta tags of the page, we can set the properties that are available in a meta element.

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 *