文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>Ajax的使用体验

Ajax的使用体验

时间:2010-09-11  来源:hlhcto

Ajax其实很简单,我现在就将用Ajax技术开发时用的方法和步聚分享给大家

 

  1. //一、创建xmlhttp对象  
  2. function createXMLHttpRequest()  
  3. {  
  4.   if(window.ActiveXObject){  
  5.     //MS IE  
  6.     try{  
  7.         xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");   
  8.     }catch(e){  
  9.         try{  
  10.             xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");  
  11.         }catch(e){ }  
  12.     }   
  13. }else if(window.XMLHttpRequest){  
  14.     //NOT MS IE  
  15.     xmlhttp=new XMLHttpRequest();  
  16.     }  
  17. if(!xmlhttp){  
  18.     xmlhttp=false;  
  19.     alert("无法建立 Ajax 请求对象,你的浏览器版本太低,建议升级你的浏览器.");  
  20.     //window.location.href="ajaxError.htm";  
  21.     }  
  22. //二、写一个公共的调用函数  
  23. var pubdiv="";  
  24. function AjaxGetContent(url,divobj)  
  25. {  
  26.   pubdiv=divobj;  
  27.   createXMLHttpRequest();  
  28.   document.getElementById(divobj).innerHTML="正在载入...";  
  29.   xmlhttp.onreadystatechange=ResultAjaxGetContent; //ajax回调函数  
  30.   xmlhttp.open("post",url,true);  
  31.   xmlhttp.send(null)  
  32. }  
  33. //ajax回调函数  
  34. function ResultAjaxGetContent()  
  35. {  
  36.   if (xmlhttp.readyState==4)  
  37.   {  
  38.     if (xmlhttp.status==200)  
  39.     {  
  40.       document.getElementById(pubdiv).innerHTML=xmlhttp.responseText;  
  41.      }        
  42.    }  
  43. }  
  44. //三、写具体的事件功能函数,调用上面定义的ajax公用函数实现具体功能  
  45. 如  
  46. //div_car:为页面上要显示购物车信息的图层ID  
  47. function AjaxShowCart()  
  48. {  
  49.   var url="cart.asp?userid=8";  
  50.   AjaxGetContent(url,"div_cart")  
  51. }  
  52. //通过以上三步就可使用AJAX功能了!现在有很多的ajax框架可用,但个人觉得在很多的时候那些显得有些庞大的框架根本就用不着,自己写的代码更实用又方便! 

 

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载