欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Ajax 數(shù)據(jù)請(qǐng)求的簡(jiǎn)單分析

 更新時(shí)間:2011年04月05日 21:42:51   作者:  
Ajax使用的關(guān)鍵對(duì)象是XmlHttpRequest對(duì)象,但是涉及到跨瀏覽器的的問(wèn)題,所以,需要?jiǎng)?chuàng)建一個(gè)具兼容性的對(duì)象
比如:
復(fù)制代碼 代碼如下:

function xmlHttpR(){
var xmlhttp;
if(window.XMLHttpRequest){
xmlhttp=new XMLHttpRequest();
}
else{
try{xmlhttp=new ActiveXObject("Msxml2.XMLHTTP")}
catch(e){
try{xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e){return null;
}
}
return xmlhttp;

這樣就基本上能創(chuàng)建一個(gè)跨瀏覽器的對(duì)象了;
下面是ajax的簡(jiǎn)單運(yùn)用,利用XmlHttpRequest對(duì)象完成;
復(fù)制代碼 代碼如下:

var ajaxEl=new Object();
//ajaxEl是自定義的命名空間;
ajaxEl.contentLoad=function(url){
//IE瀏覽器下,會(huì)啟用緩存,這里url加入date字段就是為了防止IE使用緩存,當(dāng)然也可以使用Math.random()產(chǎn)生和getTime類似的效果;
url+="?date="+new Date().getTime();
this.req=null;
this.url=url;
//這個(gè)回調(diào)函數(shù)就是在數(shù)據(jù)在頁(yè)面上的更新函數(shù);
this.onload=function(){
//domEl是ID為#test的dom元素;
var domEl=document.getElementById("test");
//除了用responseText屬性,也可以使用responseXml獲得一張數(shù)據(jù)表;
domEl.innerHTML=this.req.responseText;
}
this.Xmlhttp(url);
}
ajaxEl.contentLoad.prototype={
Xmlhttp:function(url){
if(window.XMLHttpRequest){
this.req=new XMLHttpRequest();
}
else{
try{this.req=new ActiveXObject("Msxml2.XMLHTTP")}
catch(e){
try{this.req=new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e){return null;
}
}
}
if(this.req){
var xmlR=this;
this.req.onreadystatechange=function(){
if(xmlR.req.readyState===4){
xmlR.onload.call(xmlR);
}
}
this.req.open("GET",url,true);
this.req.send(null);
}
}
}
var xmlE=new ajaxEl.contentLoad("main.php");

main.php里面,我這里設(shè)置的比較簡(jiǎn)單的示例代碼:在頁(yè)面上就會(huì)顯示一個(gè)類似:now! time is:05:18:10 am 2011,這樣可動(dòng)態(tài)變化的時(shí)間。
復(fù)制代碼 代碼如下:

echo "now! time is:".date("H:i:s a Y");

相關(guān)文章

最新評(píng)論