How to change the value of the meta tag?

Asked

Viewed 986 times

2

I am trying to change the value of meta tags with data coming from the database but they are not filled inside the tags. The value is recovered normally and displayed within the script when the view-source is activated but the content of the meta tag remains empty.

<script type="text/javascript">
$(function(){
    $('meta[name="author"]').attr('content','Marcelo de Andrade');
    $('meta[name="description"]').attr('content','zzzzzzzzz');
    $('meta[name="keywords"]').attr('content','');

});
</script>

<meta name="author" content="" />
<meta name="description" content="" />
<meta name="keywords" content="" />
<meta property="og:title" content=""/>
<meta property="og:url" content=""/>

<script type="text/javascript">
    $(function(){
        $('meta[name="author"]').attr('content','<?php echo $owner->name ?>');
        $('meta[name="description"]').attr('content','<?php echo $group->elevatorpitch ?>');
        $('meta[name="keywords"]').attr('content','<?php echo $group->tags ?>');
        $('meta[property="og:title"]').attr('content','<?php echo $group->name ?>');
        $('meta[property="og:url"]').attr('content','<?php echo ideas_url($group) ?>');
    });
</script>

1 answer

6


"View source" shows the response served for the HTTP request. It is not possible to change the content of this response using a client-side language.

Javascript alters the GIFT which is the structural representation of the document. You can view a serialized representation of the DOM through the developer tools (F12 in Chrome, Firefox or IE), where you will see its elements meta possess the content updated by the JS.


That being said, several crawlers do not perform (or perform limited) Javascript, so for the purpose of SEO it is more recommended to print the content of the meta tags directly in the original source:

<meta name="author" content="<?php echo $owner->name ?>" />
...
  • 2

    However, it’s still worth remembering that Google parses both Javascript and CSS to find out what happens on the page and to give relevance. Matt Cutts himself has already talked about it here: http://www.youtube.com/watch?v=B9BWbruCiDc

  • 1

    @Ronnyamarante yes, Google works over witchcraft. ;) Pranks aside, I hadn’t seen this video yet, thanks for the info. =]

  • Thank you, guys!

Browser other questions tagged

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