Home > OS >  I'm trying to preview random photos on the site's share link
I'm trying to preview random photos on the site's share link

Time:11-17

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]/>

CodePudding user response:

"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>

  • Related