Search code examples
htmlmeta-tags

How to output the value of a meta tag in a web page


in my web page I have a meta

<meta name="version" content="2.42">

I would like to output the value of the content attribute somewhere in the page to avoid having two times the same value somewhere in the code. Like

<p>&copy; Copyright 2020, Ver. myVersionNumber</p>

and myVersionNumber should be a reference to the content attribute of the meta "version". How could I achieve this?

Thanks in advance and best regards


Solution

  • You can access the meta tag value using JavaScript:

    const versionElem = document.querySelector('meta[name="version"]');
    document.getElementById('version').textContent = versionElem.content;
    <meta name="version" content="2.42">
    
    <p>&copy; Copyright 2020, Ver. <span id='version'>myVersionNumber</span></p>