Hamed Minaee
Hamed Minaee

Reputation: 2560

Cannot insert the meta tag at the beginning of a head

I am trying to add a script into the beginning of a head in a react app. The only way I could think of it was adding the following to component did mount:

    var head = document.getElementsByTagName("head")[0];
    head.insertBefore("<meta  id='test'/>", head.firstElementChild);
    console.log("test");
    console.log(head);

which I found here:

How do I insert a script tag in to the top/beginning of head tag?

But I cannot see anything inserted into the head. Any idea?

Upvotes: 0

Views: 467

Answers (1)

SLaks
SLaks

Reputation: 887459

insertBefore() takes an Element, not a string.

You need to create an Element using document.createElement().

Upvotes: 1

Related Questions