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>© Copyright 2020, Ver. <span id='version'>myVersionNumber</span></p>
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>© Copyright 2020, Ver. <span id = "v1"></span></p>
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>© 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.
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>© 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
@Rob: thank you for this link. Does not help since: don’t use PHP and the web page is to be published.
@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.
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.