耐人寻味

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
 1var net=new Object();
 2net.READY_STATE_UNINITIALIZED=0;
 3net.READY_STATE_LOADING=1;
 4net.READY_STATE_LOADED=2;
 5net.READY_STATE_INTERACTIVE=3;
 6net.READY_STATE_COMPLETE=4;
 7net.ContentLoader=function(url,onload,onerror){
 8this.url=url;
 9this.req=null;
10this.onload=onload;
11this.onerror=(onerror) ? onerror : this.defaultError;
12this.loadXMLDoc(url);
13}
14net.ContentLoader.prototype={
15loadXMLDoc:function(url){
16if (window.XMLHttpRequest){
17this.req=new XMLHttpRequest();
18} else if (window.ActiveXObject){
19this.req=new ActiveXObject("Microsoft.XMLHTTP");
20}
21if (this.req){
22try{
23var loader=this;
24this.req.onreadystatechange=function(){
25loader.onReadyState.call(loader);
26}
27this.req.open('GET',url,true);
28this.req.send(null);
29}catch (err){
30this.onerror.call(this);
31}
32}
33},
34onReadyState:function(){
35var req=this.req;
36var ready=req.readyState;
37if (ready==net.READY_STATE_COMPLETE){
38var httpStatus=req.status;
39if (httpStatus==200 || httpStatus==0){
40this.onload.call(this);
41}else{
42this.onerror.call(this);
43}
44}
45},
46defaultError:function(){
47alert("error fetching data!"
48+"\n\nreadyState:"+this.req.readyState
49+"\nstatus: "+this.req.status
50+"\nheaders: "+this.req.getAllResponseHeaders());
51}
52}
posted on 2006-06-08 15:04  martinfly  阅读(158)  评论(0编辑  收藏  举报