Solution 1 :

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>

Solution 2 :

Use JS to retrieve value and then put it to required tag like this:

var metaElementCollection = document.getElementsByTagName('meta');

  for (let i = 0; i < metaElementCollection.length; i++) {
    if (metaElementCollection[i].getAttribute('name') === "version") {
      document.getElementById('v1').textContent = metaElementCollection[i].getAttribute('content');
    }
  }
  
<meta name="version" content="2.42">
<p>&copy; Copyright 2020, Ver. <span id = "v1"></span></p>

Solution 3 :

As stated earlier, you can apply document.querySelector('meta[name="version"]') to get the meta element via Javascript, but, even though that would answer your question, per se, I would not recommend to do that, because that’s a hack, which relies on Javascript being switched on in the browser and your meta tag maintaining its name. Instead, make sure that you have a server-side variable for the version which is outputted into both your meta tag and your paragraph. Since you have not given any information about your server technology, my example might be incompatible with your tech stack, but nevertheless, whatever tech stack is in use for your project, you can apply this idea. Assuming that you use PHP:

<?php
    $version = "2.42";
?>

<!-- Some HTML -->

<meta name="version" content="<?php echo $version; ?>">

<!-- Some HTML -->

<p>&copy; Copyright <?php echo date("Y"); ?>, Ver. <?php echo $version; ?></p>

Notice in the code above that I have ensured that the year is dynamic as well, since I assume that on New Year’s eve you do not want to override the year each year.

Problem :

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

Comments

Comment posted by stackoverflow.com/questions/16305595/…

Check this:

Comment posted by How can I retrieve information from the tag and display it on a webpage?

Does this answer your question?

Comment posted by Humbalan

@Rob: thank you for this link. Does not help since: don’t use PHP and the web page is to be published.

Comment posted by Humbalan

@BatLouhan thank you for your help. I tested and it worked well. Anyhow I selected the solution of Sid Vishnoi, since I liked its shortness.

Comment posted by Lajos Arpad

A correct solution, upvoted for that. However, we do not want to do this in Javascript in the browser, it makes much more sense to generate that paragraph on server-side.

By

Leave a Reply

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