Connecting Tech Pros Worldwide Help | Site Map

changing meta tag with javascript

Newbie
 
Join Date: Oct 2007
Posts: 21
#1: Aug 29 '08
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.com/share_partners.php (Please go to this to see what I mean)

<meta name="title" content="Smith hails 'unique' Wable legacy" />
<meta name="description" 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's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,642
#2: Aug 29 '08

re: changing meta tag with javascript


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