Run Javascript within meta tag parameter

Asked

Viewed 52 times

1

Is there any way to do what I said in the title? I tried it this way but it seems to me that it does not perform the function:

<script>
function imgPrincProdu(){
    return $('.flickity-slider > img').attr('src');
}
</script>
<meta property="og:image" content="javascript:imgPrincProdu()" />
  • Why don’t you assign a id to the tag and does in JS something like setAttribute("content", "novo valor")?

  • You can give an example, Anderson, if it’s not too much trouble?

  • It would be <meta id="meuid"> and in JS I take #meuid and add the value?

  • I already got in touch with the platform and do direct in PHP, there became more dynamic and functional. Thank you, Anderson!

1 answer

2


As I said, just assign one id for your tag and use the method setAttribute to set the new value. See the example below, where you assign "new value" to the attribute content of tag:

const ogImage = document.getElementById("og-image");

ogImage.setAttribute("content", "Novo valor");
<meta id="og-image" property="og:image" content="" />

To verify that the value has been changed just inspect the page and search the tag within the body of snippet. If it all worked out, you’ll see content="Novo valor".

Browser other questions tagged

You are not signed in. Login or sign up in order to post.