2

I need to make dynamic meta. on click or on load the page, the field of metatags have to take the content inside tag <h1>. it's possible?

<html>
  <head> 

    <title>my site</title>
    <meta property="og:title" content="" />
    <meta property="og:description" content="" />
    <meta property="og:image" content="" />

  </head>
  <body>

    <h1 id="titolo">Titolo</h1>

    <p id="descrizione">Vestibulum velit justo, porta a aliquet a; sodales nec quam. Etiam lobortis tortor ex, non iaculis dolor interdum in!</p>

    <img id="immagine" src="immagine.jpg" />

    <a target='_blank' class="fb"href='https://www.facebook.com/sharer/sharer.php?u=http://miosito.org/' >Share facebook</a>
    <a target='_blank' class="tw" href='https://twitter.com/intent/tweet?url=http://miosito.org/ '>Share twitter</a>
    <a target='_blank' class="gplus" href='https://plus.google.com/share?url=http://miosito.org/' >Share gplus</a>

  </body>
</html>

thanks

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
manu
  • 77
  • 7

2 Answers2

2

To achieve this with jQuery try this:

$('meta[property=og:title]').attr('content', 'new content');

where 'new content' is the content you wish to place inside it.

Edit: To do exactly what you want to do and set the content to what's in the H1 you might try:

$('meta[property=og:title]').attr('content', $('#titolo').html());
Drummad
  • 722
  • 1
  • 6
  • 23
  • I've tried but don't work – manu Jun 19 '15 at 14:08
  • $('meta[property="og:title]"'), but I do not think that it makes sense your question, because, when you update the tag og, facebook has already parsed the page with the actual value, not the replaced, as @Guruprasad Rao mentions in the comment. – Giacomo Paita Jun 19 '15 at 14:09
  • See here, for example: http://stackoverflow.com/questions/29127891/dynamic-meta-tags-in-html5-and-javascript-jquery?rq=1 – Giacomo Paita Jun 19 '15 at 14:18
  • – manu Jun 19 '15 at 14:19
0

It's been answered before but you could do something like this:

<script type='text/javascript'>
$(document).ready(function() {
    $('meta[property=og:title]').remove();
    $('head').append('<meta property="og:title" content="blah" />');
});
</script>
Nick
  • 2,877
  • 2
  • 33
  • 62