<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
<script type="text/javascript">
var xmlHttp;
var key;
var bar_color = 'gray';
var span_id = "block";
var clear = " "
var counter;
function createXMLHttpRequest()
{
if (window.ActiveXObject)
{
try
{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e)
{
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
}
}
else
xmlHttp = new XMLHttpRequest();
}
function go() {
createXMLHttpRequest();
checkDiv();
counter=1;
if(xmlHttp)
{
var url = "http://"+location.host+"/progressbar/Handler.ashx?task=create";
var button = document.getElementById("go");
button.disabled = true;
xmlHttp.open("GET", url, true);
xmlHttp.onreadystatechange = function()
{
if (xmlHttp.readyState == 4)
{
if (xmlHttp.status == 200)
{
setTimeout("pollServer()", 2000);
}
}
}
xmlHttp.send(null);
}
}
function pollServer() {
createXMLHttpRequest();
alert(counter);
var url = "http://"+location.host+"/progressbar/Handler.ashx?task=poll&counter="+counter;
xmlHttp.open("GET", url, true);
xmlHttp.onreadystatechange = function()
{
if (xmlHttp.readyState == 4)
{
if (xmlHttp.status == 200)
{
var percent_complete = xmlHttp.responseXML.getElementsByTagName("percent")[0].firstChild.data;
var index = processResult(percent_complete);
for (var i = 1; i <= index; i++)
{
var elem = document.getElementById("block" + i);
elem.innerHTML = clear;
elem.style.backgroundColor = bar_color;
var next_cell = i + 1;
if (next_cell > index && next_cell < 10)
{
document.getElementById("block" + next_cell).innerHTML = percent_complete + "%";
}
}
if (index < 9)
{
counter++;
function processResult(percent_complete) {
var ind;
if (percent_complete.length == 1) {
ind = 1;
} else if (percent_complete.length == 2) {
ind = percent_complete.substring(0, 1);//取第一位数
} else {
ind = 9;
}
return ind;
}
function checkDiv() {
var progress_bar = document.getElementById("progressBar");
if (progress_bar.style.visibility == "visible") {
clearBar();
document.getElementById("complete").innerHTML = "";
} else {
progress_bar.style.visibility = "visible"
}
}
function clearBar() {
for (var i = 1; i< 10; i++) {
var elem = document.getElementById("block" + i);
elem.innerHTML = clear;
elem.style.backgroundColor = "white";
}
}
</script>
</head>
<body>
<div>
<h1>Ajax Progress Bar Example</h1>
Launch long-running process: <input type="button" value="Launch" id="go" onclick="go();"/>
</div>
<table align="center" >