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

后端 未结 18 802
后悔当初
后悔当初 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:38

    simple add and div atribute to each meta tag example

    <meta id="mtlink" name="url" content="">
    <meta id="mtdesc" name="description" content="" />
    <meta id="mtkwrds" name="keywords" content="" />
    

    now like normal div change for ex. n click

    <a href="#" onClick="changeTags(); return false;">Change Meta Tags</a>
    

    function change tags with jQuery

    function changeTags(){
       $("#mtlink").attr("content","http://albup.com");
       $("#mtdesc").attr("content","music all the time");
       $("#mtkwrds").attr("content","mp3, download music, ");
    
    }
    
    0 讨论(0)
  • 2020-11-22 14:40

    Yes, you can do that.

    There are some interesting use cases: Some browsers and plugins parse meta elements and change their behavior for different values.

    Examples

    Skype: Switch off phone number parser

    <meta name="SKYPE_TOOLBAR" content="SKYPE_TOOLBAR_PARSER_COMPATIBLE">
    

    iPhone: Switch off phone number parser

    <meta name="format-detection" content="telephone=no">
    

    Google Chrome Frame

    <meta http-equiv="X-UA-Compatible" content="chrome=1">
    

    Viewport definition for mobile devices

    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    

    This one can be changed by JavaScript. See: A fix for iPhone viewport scale bug

    Meta description

    Some user agents (Opera for example) use the description for bookmarks. You can add personalized content here. Example:

    <!DOCTYPE html>
    <title>Test</title>
    <meta name="description" content="this is old">
    <script type='text/javascript' src='http://code.jquery.com/jquery-1.8.2.js'></script>
    
    <button>Change description</button>
    
    <script type='text/javascript'>
    $('button').on('click', function() {
        // Just replacing the value of the 'content' attribute will not work.
        $('meta[name=description]').remove();
        $('head').append( '<meta name="description" content="this is new">' );
    });
    </script>
    

    So, it’s not just about search engines.

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

    Yes, it is.

    E.g. to set the meta-description:

    document.querySelector('meta[name="description"]').setAttribute("content", _desc);
    
    0 讨论(0)
  • 2020-11-22 14:47
    var metaTag = document.getElementsByTagName('meta');
    for (var i=0; i < metaTag.length; i++) {
        if (metaTag[i].getAttribute("http-equiv")=='refresh')
            metaTag[i].content = '666';
        if (metaTag[i].getAttribute("name")=='Keywords')
            metaTag[i].content = 'js, solver';
    }
    
    0 讨论(0)
  • 2020-11-22 14:48

    It is definitely possible to use Javascript to change the meta tags of the page. Here is a Javascript only approach:

    document.getElementsByTagName('meta')["keywords"].content = "My new page keywords!!";
    document.getElementsByTagName('meta')["description"].content = "My new page description!!";
    document.title = "My new Document Title!!";
    

    I have verified that Google does index these client side changes for the code above.

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

    If we don't have the meta tag at all, then we can use the following:

    var _desc = 'Out description';
    var meta = document.createElement('meta');
    meta.setAttribute('name', 'description');
    meta.setAttribute('content', _desc);
    document.getElementsByTagName('head')[0].appendChild(meta);
    
    0 讨论(0)
提交回复
热议问题