zoukankan      html  css  js  c++  java
  • onreadystatechange在FF上当状态是4不执行回调事件

    原:
    //创建XMLHTTP
    function CreateXmlHttp() 
    {   
    	var _xmlhttp;
    	try 
    	{ 
    			_xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
    	}
    	catch (e) 
    	{
    		try 
    		{
    		   _xmlhttp=new XMLHttpRequest(); 
    		}
    		catch (e) 
    		{
    		   _xmlhttp=false; 
    		}
    	}
    	return _xmlhttp; 
    }
    
    //根据房态动态修改提交按钮
    function GetRoomState(_inTime,_outTime,_roomId)
    {
       var xmlhttp1=CreateXmlHttp();
       if (xmlhttp1) 
       {    
            var tobj=document.getElementById("tijiao");
    	    xmlhttp1.open("GET", "/hotel/getRoomState.asp?intime="+_inTime+"&outtime="+_outTime+"&roomid="+_roomId+"&d="+(new Date()).getTime(), false);
            
    		xmlhttp1.onreadystatechange=function() 
    		{ 
    			if (xmlhttp1.readyState==4 && xmlhttp1.status==200) 
    			{ 
    				var isFull=unescape(xmlhttp1.responseText);
    				if(isFull==1)
    				{
    				   //满 
    				   if(tobj!=null)
    				   {
    				     tobj.innerHTML="很抱歉,您选择的酒店房型此期间已满,请重新选择!";
    				   }
    				}
    				else
    				{
    				  //不满 
    				   if(tobj!=null)
    				   {
    				     tobj.innerHTML="<img  src=\"/image/btn_tijiao.jpg\" onclick=\"SubmitForm('order_save.asp',form2,'order')\" onMouseOver=\"this.src='/image/btn_tijiao2.jpg'\" onMouseOut=\"this.src='/image/btn_tijiao.jpg'\" style=\"cursor:hand;\"/>";
    				   }
    				}
    			}
    		}
    		xmlhttp1.send(null); 
    	}   
    }
     
    修改版:
    //创建XMLHTTP
    function CreateXmlHttp() 
    {   
    	var _xmlhttp;
    	try 
    	{ 
    			_xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
    	}
    	catch (e) 
    	{
    		try 
    		{
    		   _xmlhttp=new XMLHttpRequest(); 
    		}
    		catch (e) 
    		{
    		   _xmlhttp=false; 
    		}
    	}
    	return _xmlhttp; 
    }
    
    function getOs() 
    { 
    	if(navigator.userAgent.indexOf("MSIE")>0) 
    	{ 
    		return "MSIE"; //IE浏览器 
    	} 
    	if(isFirefox=navigator.userAgent.indexOf("Firefox")>0)
    	{ 
    		return "Firefox"; //Firefox浏览器 
    	} 
    } 
    
    var xmlhttp2;
    
    //根据房态动态修改提交按钮
    function GetRoomState(_inTime,_outTime,_roomId)
    {
       xmlhttp2=CreateXmlHttp();
       if (xmlhttp2) 
       {    
            
    		var btype=getOs(); //获取浏览器
    		xmlhttp2.onreadystatechange=(btype=="Firefox")?IsBookRoom():IsBookRoom; 
    	    xmlhttp2.open("GET", "/hotel/getRoomState.asp?intime="+_inTime+"&outtime="+_outTime+"&roomid="+_roomId+"&d="+(new Date()).getTime(), false);
    		xmlhttp2.send(null); 
    		xmlhttp2.onreadystatechange=(btype=="Firefox")?IsBookRoom():IsBookRoom; 
    		
    	}   
    }
    
    function IsBookRoom()
    {
                var tobj=document.getElementById("tijiao");
    			if (xmlhttp2.readyState==4 && xmlhttp2.status==200) 
    			{ 
    			    
    				var isFull=unescape(xmlhttp2.responseText);
    				if(isFull==1)
    				{
    				   //满 
    				   if(tobj!=null)
    				   {
    				     tobj.innerHTML="很抱歉,您选择的酒店房型此期间已满,请重新选择!";
    				   }
    				}
    				else
    				{
    				  //不满 
    				   if(tobj!=null)
    				   {
    				     tobj.innerHTML="<img  src=\"/image/btn_tijiao.jpg\" onclick=\"SubmitForm('order_save.asp',form2,'order')\" onMouseOver=\"this.src='/image/btn_tijiao2.jpg'\" onMouseOut=\"this.src='/image/btn_tijiao.jpg'\" style=\"cursor:hand;\"/>";
    				   }
    				}
    			}
    }
    
    
     xmlhttp2.open("GET", "/hotel/getRoomState.asp?intime="+_inTime+"&outtime="+_outTime+"&roomid="+_roomId+"&d="+(new Date()).getTime(), true);
    在火狐上显示,如果修改成 xmlhttp2.open("GET", "/hotel/getRoomState.asp?intime="+_inTime+"&outtime="+_outTime+"&roomid="+_roomId+"&d="+(new Date()).getTime(), false);
    就不显示了。
    原因分析: 
    其一:这时不能用xmlHttp.send(),需要内容,如果没有内容,要用NULL 
    其二:在ajax的XMLHttpRequest.onreadystatechange方法的差异:在FF中当状态为1(即XMLHttpRequest已经调用open但还没有调用send时),FF则会继续执行onreadystatechange后面的代码,到执行完后面的代码后,在执行onreadystatechange在状态2,3,4的代码,而IE会等待状态2的到了,执行完onreadystatechange中状态2,3,4的代码后,继续执行后面的代码,这样问题就出现了,经常我们在onreadystatechange的代码要处理从服务器上获得的数据(这个数据只有在onreadystatechange的状态为4时,才可以得到),所以这在IE中不存在问题,因为它会等待onreadystatechange状态4到来以后,在执行onreadystatechange后面的数据,但是由于FF不会等到onreadystatechange状态4到来后在执行onreadystatechange后面的代码,所以后面的代码就不能处理从服务器上获得的数据
    在火狐上回调有2种,一种需要函数带(),另一种不需要带。
  • 相关阅读:
    Mac下安装svn服务器
    php 当前日期加一天和指定日期加一天
    MariaDB与MySQL
    JS中判断null、undefined与NaN的方法
    PHP保留两位小数的几种方法
    jquery的cookie插件
    mysql(5.7以上)查询报错:ORDER BY clause is not in GROUP BY..this is incompatible with sql_mode=only_full_group_by
    MySQL数据的导出和导入
    qrCode二维码字符串长度太多压缩的问题
    解决 img 标签上下出现的间隙
  • 原文地址:https://www.cnblogs.com/merrick/p/1802187.html
Copyright © 2011-2022 走看看