热门:网页模板.net视频教程JQueryMVCjsonExtJs源码示例三级联动JQuery菜单
您现在的位置:.Net中文社区>> AJAX编程>>正文内容

XMLHttpRequest ajax调用无刷新显示后台时间

发布时间:2010年03月04日点击数: 佚名
  1. <html xmlns="http://www.w3.org/1999/xhtml" > 
  2. <head runat="server"> 
  3.     <title>无标题页</title> 
  4. </head> 
  5. <script type="text/javascript" > 
  6.     var timeout = null; //setInterval函数句柄    
  7.     var xmlHttp = false; //  
  8.     function SendRequest() 
  9.     { 
  10.         //xmlHttp = false;  
  11.         if (window.ActiveXObject) 
  12.         {    
  13.             try  
  14.             { 
  15.                 xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); 
  16.             }     
  17.             catch (e)  
  18.             { 
  19.                 xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); 
  20.             }    
  21.         } 
  22.         else if (window.XMLHttpRequest)  
  23.         { 
  24.             xmlHttp = new XMLHttpRequest(); 
  25.         }  
  26.         else 
  27.         { 
  28.             alert('初始化错误!'); 
  29.             return; 
  30.         } 
  31.         var url = "Handler.ashx"
  32.         //var url = "Default2.aspx"
  33.         xmlHttp.open("GET",url,true); 
  34.         xmlHttp.onreadystatechange = ShowData
  35.         xmlHttp.send(null); 
  36.     } 
  37.     function ShowData() 
  38.     { 
  39.         if(xmlHttp.readystate == 4) 
  40.         { 
  41.             if(xmlHttp.status == 200) 
  42.             { 
  43.                 var tag = document.getElementById("container"); 
  44.                 tag.innerHTML = ""
  45.                 tag.innerHTML = xmlHttp.responseText; 
  46.             }   
  47.         } 
  48.     } 
  49.  
  50.     //开始自动刷新    
  51.     function Update()  
  52.     {    
  53.         timeout = window.setInterval("SendRequest()", 1000);//设定1秒调用一次Default2.aspx页面    
  54.     }    
  55.     //停止自动刷新    
  56.     function StopUpdate()  
  57.     {    
  58.         if (timeout != null)  
  59.         {    
  60.             window.clearInterval(timeout);    
  61.         }    
  62.     } 
  63. </script> 
  64. <body onload="SendRequest();"> 
  65.     <form id="form1" runat="server"> 
  66.     <div> 
  67.         <input type="button" value="Start Fresh" onclick="Update();"/>   
  68.         <input type="button" value="Stop Fresh" onclick="StopUpdate();"/>  
  69.         <input id="Button1" type="button" value="确定" onclick="SendRequest();"/> 
  70.     </div> 
  71.     </form> 
  72.     <div id="container"><!--容器--></div> 
  73. </body> 
  74. </html> 

本站热点业务

更多模板/案例展示

关于我们 | 联系我们 | 团队日志 | 网站地图 | 网站合作