DelphiFAQ Home Search:

Dynamically change a normal text statement on a html page

 

comments3 comments. Current rating: 4 stars (2 votes). Leave comments and/ or rate it.

Question:

I want to display a kind of countdown on my web page with a redirect happening once 0 is reached.
How can I update the counter without reloading the page every time?

Answer:

You need to define a
section or even use a header. Give this section a unique ID, use javascript to get a reference to the element and use the .innerHTML property to update the text.

To implement the countdown, you need to set a timer.
The example shows this and uses setTimeout for the timer.

<script language="JavaScript">
<!--
var iTime=5;
function update() {
var txt = 'Will update progress ';
if (iTime>1) { txt = txt + 'in ' + iTime + ' seconds.'; }
else {
  if (iTime==1) { txt = txt + 'in one second.'; }
  else { txt = txt + 'now.'; }
}
document.getElementById("cnt").innerHTML = txt;
iTime=iTime-1;
if(iTime>=0) { ID=window.setTimeout("update();",1000); }
else { document.location.href="/request?70"; }
} 
// -->

</script>
</head>
<body onload="update()">

<div name=cnt id=cnt>Will update progress in 5 seconds.</div>

Comments:

2007-04-22, 01:10:09
anonymous from Iran in Chahar Mahall va Bakhtiari, Iran  
rating
I used it very simply.
2008-06-17, 05:25:38
from India  
asdfd
2008-09-10, 02:33:08
darksystemcd@bol.com.br from Santos, Brazil  
rating
I have a question about this JavaScript code ....

What about when if i want to write text direct to that var :
'var txt = 'Will update progress ';'

then to be able for change a UNKNOW text string placed at runtime
withnot realod the entire page .
just like the function gets the time,a place where it can get a string .

something like :
'var MyStringFromUnknowRssFeed;' <<But from where it came from ?

Then works like this :

<div name=cnt id=cnt>its realtime rss feed</div> <<<Updated right here ?!!

 

 

Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option.
 
It seems that you are
from Los Angeles, US .

Info/ Feedback on this

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:
photo Add a picture:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity. The owner of this web site reserves the right to delete such material.