RSS

Using javascript Attribute to set meta tags dynamically

26 Jul

Set meta tags content dynamically using javascript setAttribute.

Example for setAttribute : 

<meta id="description" name="description" 
content="example meta description content" />
<meta name="keywords" content="example meta keywords content" />
<meta name="author" content="example author conent" />
<title>example title content</title>
<script type="text/javascript">
 window.onload = function () {
     setProductMeta();
 }
function setProductMeta(){
 document.getElementById("description").setAttribute("content",
"dynamic meta description");
 document.getElementById("keywords").setAttribute("content",
"dynamic meta description");
 document.title = 'dynamic meta title';
}
</script>

Example for getAttribute : 

<meta id="description" name="description" 
content="example meta description content" />
<meta name="keywords" content="example meta keywords content" />
<meta name="author" content="example author conent" />
<script type="text/javascript">
window.onload = function () {
 setProductMeta();
}
function setProductMeta(){
 var metaDesc = document.getElementById("description")
.getAttribute("content");
 var metaKeys = document.getElementById("keywords")
.getAttribute("content");
 var metaAuthor = document.getElementById("author").getAttribute("content"); 
 document.getElementById("demoDesc").innerHTML= metaDesc;
 document.getElementById("demoKeys").innerHTML= metaKeys;
 document.getElementById("demoAuthor").innerHTML= metaAuthor;
}
</script>
<div id="demoDesc"></div>
<div id="demoKeys"></div>
<div id="demoAuthor"></div>
 
5 Comments

Posted by on July 26, 2012 in General, Javascript

 

Tags:

5 responses to “Using javascript Attribute to set meta tags dynamically

  1. pisey

    May 23, 2013 at 1:17 am

    but when i view source i still see the old content of meta description and keyword…could you give me some code of it??? thank in advance

     
  2. slots capital online casino

    July 3, 2013 at 4:21 am

    The 2nd component slots capital online casino we win your slots capital online casino reward
    again, you need to risk $10 000 with slots capital online casino slots.
    All you have to do is actually enroll an account provider using them, click the cashier option, in addition
    to use among the payoff unique codes while producing the actual transaction.

     
  3. responsible trading

    July 7, 2013 at 5:39 pm

    By simply watching what is the news releases of the
    day time, financial reports, or perhaps the most up-to-date in firmly trending promotes, you
    may use this data in order to estimate which usually technique a place may unavoidably
    proceed. They have got a great 81% return and offer shareholders having a
    well-established as well as successful Binary responsible trading podium.
    responsible trading offers a expert but straightforward services intended for responsible trading professionals.

     
  4. Loans

    December 25, 2013 at 1:14 pm

    I created an Arduino DC Motor class and a few keywords are properly highlighted (such as the name of the class ArduinoDCMotor and member functions stop

     
  5. Sokken

    May 22, 2015 at 11:59 am

    Didn’t seem to work for me…

     

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: