Alex
Alex

Reputation: 31

How to add a meta tag to the <head> after page loaded

I want to dynamically add a meta tag to my HTML <head> after the page is loaded. I found some solutions to add a meta tag, but the all only work while the page is loading. After the page is loaded once, the don't work anymore.

document.head.innerHTML += '<meta content="test" property="test"/>';

That's what I tried. But as I said above this does not work is the page is already loaded.

Is there a solution for this? Maybe something like reload the page?

Upvotes: 1

Views: 1833

Answers (1)

Petr Nečas
Petr Nečas

Reputation: 485

function ready() { 
    document.head.innerHTML += '<meta content="test" property="test"/>';
}
document.addEventListener("DOMContentLoaded", ready);

Maybe event DOMContentLoaded is the solution, source: https://javascript.info/onload-ondomcontentloaded.

Upvotes: 1

Related Questions