关于onreadystatechange属性(转)

关于onreadystatechange属性(转)

在编写Ajax方法的时候,我们经常会写上类似于这样的代码:
Javascript代码 复制代码
  1. <script type="text/javascript">   
  2. var xmlHttp;   
  3. //创建一个XmlHttpRequeset对象   
  4. function createXMLHttpRequest()...{   
  5.     if(window.ActiveXObject)...{   
  6.         xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");   
  7.     }   
  8.     else if(window.XMLHttpRequest)...{   
  9.         xmlHttp = new XMLHttpRequest();   
  10.     }   
  11. }   
  12. //开始一个请求   
  13. function startRequest()...{   
  14.        
  15.     createXMLHttpRequest();   
  16.     xmlHttp.onreadystatechange = handlestatechange;   
  17.     xmlHttp.open("GET""SimpleRespose.xml"true);   
  18.     xmlHttp.Send(null);   
  19. }    
  20.   
  21. function handlestatechange()...{   
  22.     if(xmlHttp.readyState == 4)...{//描述一种"已加载"状态;此时,响应已经被完全接收。   
  23.         if(xmlHttp.status == 200)...{//200表示成功收到           
  24.             alert("The Server Replied with:" + xmlHttp.responseText)   
  25.         }   
  26.     }   
  27. }               
  28. </script>  
<script type="text/javascript">
var xmlHttp;
//创建一个XmlHttpRequeset对象
function createXMLHttpRequest()...{
    if(window.ActiveXObject)...{
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else if(window.XMLHttpRequest)...{
        xmlHttp = new XMLHttpRequest();
    }
}
//开始一个请求
function startRequest()...{
    
    createXMLHttpRequest();
    xmlHttp.onreadystatechange = handlestatechange;
    xmlHttp.open("GET", "SimpleRespose.xml", true);
    xmlHttp.Send(null);
} 

function handlestatechange()...{
    if(xmlHttp.readyState == 4)...{//描述一种"已加载"状态;此时,响应已经被完全接收。
        if(xmlHttp.status == 200)...{//200表示成功收到        
            alert("The Server Replied with:" + xmlHttp.responseText)
        }
    }
}            
</script>


第一次阅读这段代码的时候,我就感到了一点点不对劲,但是说不出来什么地方不对劲。随着对Ajax代码的进一步了解,这种感觉时刻伴随着我。

    后来,我知道了这种感觉来自于什么地方。

    看看startRequest函数。我们发现xmlHttp. onreadystatechange指向了一个函数,这个函数是在xmlHttpRequest.readyState发生改变的时候触发。我们再来看startRequest函数,想象一下整个请求发送的步骤。现在我们点击一个按钮,触发了一个startRequest函数。函数往下走,第一步是createXmlHttpRequest(),它的作用是创建一个xmlHttpRequest对象,当它完毕的时候,xmlHttpRequest.readyState的值是0(window.alert跟踪得到的),程序继续往下走,xmlHttp. onreadystatechange = handlestatechange,因为状态没有改变(xmlHttpRequest.readyState的值是0),所以不触发函数,紧接着是Open()和Send(),那么,整个函数从头到尾都应该没有触发handlestatechange函数啊,但是为什么出来的结果是正确的呢?

    后来我用window.alert跟踪xmlHttp.readystate的变化,发现于原来它运行的机制是这样的。首先创建一个xmlHttpRequest的对象之后xmlHttp.readyState的值是0了,然后xmlHttp. onreadystatechange = handlestatechange没有运行。紧接着是open(),这个函数发生了之后xmlHttp.readyState的值是1了,那么就会有一个断点在Open()函数处断开,保留现场,紧接着又返回到xmlHttp. onreadystatechange = handlestatechange运行,然后再执行Send()函数,这个函数发生了之后xmlHttp.readyState的值是2了,接着又返回到xmlHttp. onreadystatechange = handlestatechange运行。以此类推。

    浏览器因为不能真正地像面向对象那么编程,所以找了个折衷的办法,但是这个办法看起来不伦不类,想了半天,再跟一个同学一起讨论,才得出这样的一个结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值