Programming C# C++ (7) Delphi (604) Java (8) JavaScript (55) Document (8) Events (8) Strings (3) perl (40) php (12) VBScript (1) Visual Basic (1)
Exchange Links About this site Links to us
|
Dynamically change a normal text statement on a html page
1 comments. Current rating: (1 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:
|