.

Changing the contents of a meta-update does not change the update time

I have meta http-equiv="refresh" inside <head> .

 <head> <meta name="mymeta" http-equiv="refresh" content="2" id="myMeta"> </head> 

Using Javascript , I am trying to change the content attribute of this meta tag.

 var myMeta = document.getElementById("myMeta"); myMeta.content="10"; 

When I display the contents through document.write(myMeta.content); I get a modified value of 10 , however the meta tag will update every 2 seconds.

I tested this in both Firefox and Opera.

FULL PAGE

 <!DOCTYPE html> <html> <head> <meta name="mymeta" http-equiv="refresh" content="2" id="myMeta"> <script> var myMeta=document.getElementById("myMeta"); myMeta.content="10"; document.write(myMeta.content); </script> </head> <body> </body> </html> 
+1
javascript html meta-tags meta
Feb 09 '13 at 16:19
source share
2 answers

This is because the browser immediately processes the <meta> tag when it is present in the download.

See DEMO .

When the document loads, the browser sees and processes the following:

 <meta name="mymeta" http-equiv="refresh" content="2" id="myMeta"/> 

Despite the fact that you are trying to change its contents from 2 to 10, this 2-second update has already been confirmed, and the browser waits 2 seconds before it refreshes the page. The 10-second refresh that JavaScript introduces does work *, although the page has been refreshed by the time it reaches 2 seconds and nothing is happening. This process repeats over and over.

Try the opposite and see what happens.

* This only works in Safari and Chrome. Firefox and Opera do not support modifying meta-updates via JavaScript.

+3
Feb 12 '13 at 14:11
source share

The getElementsByTagName method returns a NodeList , so you need to specify an index to properly access the element:

  var myMeta = document.getElementsByTagName("meta")[0]; 

As already mentioned, this probably still does not work, as the meta tag must be re-added in order to have the desired effect.

Since you use JavaScript, you can simply use setTimeout to achieve the same behavior.

 setTimeout(function() { location.reload(); },2000); // reload page after 2 seconds 
0
Feb 09 '13 at 16:21
source share



All Articles