Like what:
Copy Code code as follows:
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 the simple use of Ajax, using the XMLHttpRequest object to complete;
Copy Code code as follows:
var ajaxel=new Object ();
Ajaxel is a custom namespace;
Ajaxel.contentload=function (URL) {
IE browser, will enable caching, where the URL added to the Date field is to prevent IE use caching, of course, can also use Math.random () produce and gettime similar effect;
url+= "? date=" +new date (). GetTime ();
This.req=null;
This.url=url;
This callback function is the update function on the page of the data;
This.onload=function () {
Domel is a DOM element with an ID of #test;
var Domel=document.getelementbyid ("Test");
In addition to using the ResponseText property, 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");
Main.php inside, I set the simpler sample code here: A similar display appears on the page: now! Time Is:05:18:10 AM 2011, which can be changed dynamically.
Copy Code code as follows:
echo "now! Time is: ". Date (" H:i:s a Y ");