Is it possible to use JavaScript to change the meta-tags of the page?

后端 未结 18 801
后悔当初
后悔当初 2020-11-22 14:14

If I put a div in the head and display:none, than use JavaScript to display it, will this work?

Edit:

I have stuff loaded in AJAX. And as my AJAX changes the

相关标签:
18条回答
  • 2020-11-22 14:22

    You can change meta with, for example, jQuery calls, like this ones:

    $('meta[name=keywords]').attr('content', new_keywords);
    $('meta[name=description]').attr('content', new_description);
    

    I think it does matter for now, since google said that they will index ajax content via #!hashes and _escaped_fragment_ calls. And now they can verify it (even automatically, with headless browsers, see the 'Creating HTML Snapshots' link on the page mentioned above), so I think it is the way for hardcore SEO guys.

    0 讨论(0)
  • 2020-11-22 14:22

    For anyone trying to change og:title meta tags (or any other). I managed to do it this way:

    document.querySelector('meta[property="og:title"]').setAttribute("content", "Example with og title meta tag");
    

    Attention that the 'meta[property="og:title"]' contains the word PROPERTY, and not NAME.

    0 讨论(0)
  • 2020-11-22 14:25

    No, a div is a body element, not a head element

    EDIT: Then the only thing SEs are going to get is the base HTML, not the ajax modified one.

    0 讨论(0)
  • 2020-11-22 14:26

    meta-tags are part of the dom and can be accessed and -i guess- changed, but search-engines (the main consumers of meta-tags) won't see the change as the javascript won't be executed. so unless you're changing a meta-tag (refresh comes to mind) which has implications in the browser, this might be of little use?

    0 讨论(0)
  • 2020-11-22 14:27

    You can use more simpler and lighter solution:

    document.head.querySelector('meta[name="description"]').content = _desc
    
    0 讨论(0)
  • 2020-11-22 14:29
    $(document).ready(function() {
      $('meta[property="og:title"]').remove();
      $('meta[property="og:description"]').remove();
      $('meta[property="og:url"]').remove();
      $("head").append('<meta property="og:title" content="blubb1">');
      $("head").append('<meta property="og:description" content="blubb2">');
      $("head").append('<meta property="og:url" content="blubb3">');
    });
    
    0 讨论(0)
提交回复
热议问题