惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

云风的 BLOG
云风的 BLOG
阮一峰的网络日志
阮一峰的网络日志
有赞技术团队
有赞技术团队
小众软件
小众软件
P
Proofpoint News Feed
P
Proofpoint News Feed
Apple Machine Learning Research
Apple Machine Learning Research
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
The Last Watchdog
The Last Watchdog
O
OpenAI News
Security Latest
Security Latest
博客园 - Franky
Forbes - Security
Forbes - Security
N
Netflix TechBlog - Medium
H
Hacker News: Front Page
Cloudbric
Cloudbric
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Hugging Face - Blog
Hugging Face - Blog
Microsoft Security Blog
Microsoft Security Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
S
Security Affairs
Recent Announcements
Recent Announcements
The GitHub Blog
The GitHub Blog
S
Schneier on Security
MongoDB | Blog
MongoDB | Blog
WordPress大学
WordPress大学
Last Week in AI
Last Week in AI
博客园 - 【当耐特】
Attack and Defense Labs
Attack and Defense Labs
C
Cyber Attacks, Cyber Crime and Cyber Security
F
Fortinet All Blogs
Webroot Blog
Webroot Blog
S
Secure Thoughts
Spread Privacy
Spread Privacy
Blog — PlanetScale
Blog — PlanetScale
T
Troy Hunt's Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
V
V2EX
Security Archives - TechRepublic
Security Archives - TechRepublic
P
Privacy & Cybersecurity Law Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Simon Willison's Weblog
Simon Willison's Weblog
C
Check Point Blog
L
LINUX DO - 最新话题
NISL@THU
NISL@THU
博客园_首页
罗磊的独立博客
A
Arctic Wolf
U
Unit 42

博客园 - 逍遥游

验证数字的正则表达式集 .NET实现RSS格式文件 - 逍遥游 - 博客园 SQL SERVER 2005 通过链接服务器 访问 ORACLE 9i 的快速设定方法 [转]asp.net页面缓存技术 - 逍遥游 - 博客园 Oracle常用Script [整理]如何在 JavaScript 中实现拖放 [转]鼠标拖动层的javascript脚本 [转]Spring笔记 [转]web项目经理手册-项目经理需要铭记在心的话 [原]学习Struts+Spring+hibernate笔记 [转]Request.UrlReferrer详解 [转]hibernate产生自动增长的主键 JSP学习相关链接 C#中如何动态运行代码 .net 资源网站收集 一个很COOL的图片验证码程序[含源码] 转 关于XMLHTTP无刷新数据获取和发送 (转) .NET中获取客户端HTML代码 使用Hibernate、Struts的一些错误总结
[转]简单好用的ajax进度条(xmlhttp),实现的是真正的进度
逍遥游 · 2007-03-23 · via 博客园 - 逍遥游

代码如下:

//lael 赢动ajax简易版1.0
//时间: 2006-12-19
//http://www.gzyd.net
function Ajax(){
var XmlHttp = null;
var DataObject = null;//数据接收对象
var LoadingBar = null;//状态显示对象
var LoadingMax = 100;//进度条最大值
var LoadingWidth = null;//保存宽度,还原初始属性
var LoadingTimer = 10;//刷新时间
var LoadingTimerID = null;//时间ID
var FinishTimer = 10;//完成停留时间,等候进度条完成
var FinishTimerID = null;//时间ID
var HttpState = 0;
var CanFree = false;//释放
this.Create = function(free){
     try{
      if(free)CanFree = true;
      if(navigator.appName.indexOf("Netscape")==-1){
       try{
              XmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
       }catch(e){
        XmlHttp = new ActiveXObject("Msxml2.XMLHTTP");   
       }
          }else{
             XmlHttp=new XMLHttpRequest();
          }
       return true;
     }catch(e2){
      return false;
     }
}
this.Send = function(url, obj1, obj2){
     try{
      DataObject = obj1;
      LoadingBar = obj2;
      if(LoadingBar){
       LoadingMax = LoadingBar.offsetWidth;//保存可见宽度
       LoadingWidth = LoadingBar.style.width;//可能会没有设置宽度,这时数据为空
       LoadingBar.style.width = "0px";
       LoadingBar.innerHTML = "0%";
       LoadingBar.style.display = "";
       DataObject.style.display = "none";
       HttpState = 0;
       if(LoadingTimerID)clearInterval(LoadingTimerID);//清空时钟
       LoadingTimerID = setInterval(this.StatusBar, LoadingTimer);
      }
        XmlHttp.open("GET", url, true);    
        XmlHttp.onreadystatechange = this.StateChange;
      XmlHttp.send(null);
      return true;
     }catch(e){
      return false;
     }
}
this.StateChange = function(){
     try{
      if(XmlHttp.readyState)HttpState = XmlHttp.readyState;

        if (XmlHttp.readyState == 4) {
       if(LoadingTimerID)clearInterval(LoadingTimerID);//清空时钟
       if(LoadingBar){
        LoadingBar.style.width = LoadingWidth;
        LoadingBar.innerHTML = "100%";
        if(FinishTimerID)clearInterval(FinishTimerID);//清空时钟
        FinishTimerID = setInterval(this.Finish, FinishTimer);
       }else{
        DataObject.innerHTML = XmlHttp.responseText;
        if(CanFree)this.Destroy();
       }
        }
  
      this.Finish = function(){//放到外面访问不了
       if(FinishTimerID)clearInterval(FinishTimerID);//清空时钟
       LoadingBar.style.display = "none";
       DataObject.style.display = "";
       DataObject.innerHTML = XmlHttp.responseText;
       if(CanFree)this.Destroy();
      }
   
      this.Destroy = function(){
       if(LoadingTimerID)clearInterval(LoadingTimerID);//清空时钟
       if(FinishTimerID)clearInterval(FinishTimerID);//清空时钟
       XmlHttp = null;
      }
   
     }catch(e){}
}
this.StatusBar = function(){
     try{
      if(LoadingBar.offsetWidth >= LoadingMax){
       LoadingBar.innerHTML = "100%";
       LoadingBar.style.width = LoadingWidth;
       return;//返回
      }
   
      if(LoadingBar.offsetWidth < (HttpState + 1) * Math.floor(LoadingMax / 4)){
       var loading = LoadingBar.offsetWidth + Math.floor(LoadingMax / 40);//十分之一
       LoadingBar.style.width = loading + "px";
       var percen = Math.floor(loading / LoadingMax * 100);
       LoadingBar.innerHTML = (percen>100?100:percen) + "%";
      }else{
       LoadingBar.style.width = (HttpState + 1) * Math.floor(LoadingMax / 4) + "px";
       LoadingBar.innerHTML = Math.floor(100 / (4 - HttpState)) + "%";
      }
     }catch(e){}
}
this.Destroy = function(){
     if(LoadingTimerID)clearInterval(LoadingTimerID);//清空时钟
     if(FinishTimerID)clearInterval(FinishTimerID);//清空时钟
     XmlHttp = null;
}
}

使用一例:

<script language="jscript.encode" src="js/ajax.js"></script>
<div style="background:#b7d2ec url(http://www.gzyd.net/lael/loading.gif) center no-repeat; height:10px; text-align:center; color:#fff; " id="status">
</div>
<div id="data"></div>
<script language="javascript">
<!--
var obj = new Ajax();
if(obj.Create(true)){
obj.Send('http://www.gzyd.net', document.getElementById('data'), document.getElementById('status'));
}
//-->
</script>

////////////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////////////

//精简回调函数版

function Ajax(){
var XmlHttp = null;
var CallBackFunc = null;//回调函数
this.Create = function(){
     try{
      if(navigator.appName.indexOf("Netscape")==-1){
       try{
              XmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
       }catch(e){
        XmlHttp = new ActiveXObject("Msxml2.XMLHTTP");   
       }
          }else{
             XmlHttp=new XMLHttpRequest();
          }
       return true;
     }catch(e2){
      return false;
     }
}
//链接、回调函数
this.Send = function(url, func){
     try{
      CallBackFunc = func;
        XmlHttp.open("GET", url, true);    
        XmlHttp.onreadystatechange = this.StateChange;
      XmlHttp.send(null);
      return true;
     }catch(e){
      XmlHttp = null;
      return false;
     }
}
this.StateChange = function(){
    try{
       if(XmlHttp.readyState == 4) {
      CallBackFunc(XmlHttp.responseText, true);
      XmlHttp = null;
       }
    }catch(e){
     CallBackFunc(null, false);
     XmlHttp = null;
    }
}
}

//用法

<script language="javascript" src="js/ajax.js"></script>
<script language="javascript">
<!--
function get_html(params){
var ajax1 = new Ajax();
if(ajax1.Create()){
     ajax1.Send("ajax.php?"+params, cb);
}
}
function cb(html, success){
var div = document.createElement("DIV");
div.style.width = 100;
div.style.height = 100;
div.style.top = 100;
div.style.left = 100;
div.style.position = "absolute";
document.body.appendChild(div);
div.innerHTML = html;
}
//-->
</script>