For example:
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 ;
In this way, you can basically create a cross-browser object;
The following is a simple application of ajax, using the XmlHttpRequest object;
var ajaxEl=new Object();
//ajaxEl is a custom namespace;
ajaxEl.contentLoad=function(url){
//Under the IE browser, caching will be enabled. The date field is added to the URL here to prevent IE from using the cache. Of course, you can also use Math.random() to generate a message similar to getTime. Effect;
url ="?date=" new Date().getTime();
this.req=null;
this.url=url;
//This callback function is in the data Update function on the page;
this.onload=function(){
//domEl is the dom element with ID #test;
var domEl=document.getElementById("test");
//In addition to using the responseText attribute, you can also use responseXml to obtain a data table;
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");
In main.php, I set a relatively simple sample code here: a message similar to: now! time is:05:18:10 am 2011 will be displayed on the page, which can be changed dynamically time.
echo "now! time is:".date(" H:i:s a Y");