changing meta tag with javascript

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gagonm
    New Member
    • Oct 2007
    • 26

    #1

    changing meta tag with javascript

    Hi
    I have a scenario where I need to update meta tag content attribute at runtime using javascript .This is basically used for scenario where I m trying to integerate my application with facebook .Since facebook looks for meta tag I want to update those tags at runtime .
    www.facebook.co m/share_partners. php (Please go to this to see what I mean)

    <meta name="title" content="Smith hails 'unique' Wable legacy" />
    <meta name="descripti on" content="John Smith claims beautiful football is the main legacy of Akhil Wable's decade at the club. " />
    <link rel="image_src" href="http://www.onjd.com/design05/images/PH2/WableAFC205.jpg " />

    For ex ,In above scenarios I want to update content attribute after page loads.

    Also whether facebook will be able to read these updated values.(since SEO engines doesn't read )

    Thanks
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    you can access the meta elements with
    Code:
    var meta = document.getElementsByTagName("meta");
    then you can loop through the array and read/set the attribute values
    Code:
    var attrArray = meta[0].attributes;  // here: 1st meta element’s attributes
    var attrName = attrArray[0].nodeName; // get the name e.g. "name" (first attribute)
    attrArray[1].nodeValue = "new_attribute_value"; // set new value of "content" (2nd attribute)

    Comment

    Working...