1

I used this function in js to open the variable in the head of the html, I don't know if there is a solution or if there is another way to do it.

function randomlink() {
  var links = new Array(3)
  links[0] = "https://raw.githubusercontent.com/vonkoln/clubenoturno/clubenoturno/pr.jpeg"
  links[1] = "https://raw.githubusercontent.com/vonkoln/clubenoturno/clubenoturno/pr0.jpeg"
  links[2] = "https://raw.githubusercontent.com/vonkoln/clubenoturno/clubenoturno/pr1.jpeg"
 

  var num = Math.round(Math.random() * 2))
}
randomlink()

In html <meta property="og:image" content=links[num]/>

1 Answers1

1

"Metadata is used by browsers (how to display content or reload page), search engines (keywords), and other web services."

So changing it after the page has loaded will be useless. You will need to do this server-side wherever the content is being served from.

But if you want to try anyways, you will need to include more javascript in order to have a dynamic link. I can think of a couple ways to do it but I think your best bet will to be to just append the entire meta tag on load.

<script type='javascript'>
function escreverLinks() {
  var links = [
"https://raw.githubusercontent.com/vonkoln/clubenoturno/clubenoturno/pr.jpeg",
"https://raw.githubusercontent.com/vonkoln/clubenoturno/clubenoturno/pr0.jpeg",
"https://raw.githubusercontent.com/vonkoln/clubenoturno/clubenoturno/pr1.jpeg"
];
 
  var numero = Math.round(Math.random() * 2));

  document.head.append('<meta property="og:image" content="'+ links[numero] +'"/>')
}

document.onload(function(){
  escreverLinks()
})
</script>

Nicholas Porter
  • 2,588
  • 2
  • 23
  • 37
  • I'm not entirely sure what your goal is but I imagine you are just trying to view one of the 3 photos at random when the page loads. If so you may actually be wanting to update an element in the `` like an `` tag. – Nicholas Porter Nov 17 '22 at 01:30