The first is a function that creates Ajax:
Copy Code code as follows:
var http_request=null;
function Create_ajax () {
Http_request = false;
Start initializing the XMLHttpRequest object
if (window. XMLHttpRequest) {//mozilla browser
Http_request = new XMLHttpRequest ();
if (http_request.overridemimetype) {//Set MIME category
Http_request.overridemimetype (' Text/xml ');
}
}
else if (window. ActiveXObject) {//IE browser
try {
Http_request = new ActiveXObject ("Msxml2.xmlhttp");
catch (e) {
try {
Http_request = new ActiveXObject ("Microsoft.XMLHTTP");
catch (e) {}
}
}
if (!http_request) {//exception, failed to create object instance
Window.alert ("Cannot create XMLHttpRequest object instance.");
return false;
}
}
This function is then invoked to handle Ajax
Copy Code code as follows:
var obj1;
function Get_ajax (url,obj) {//initialization, specifying handler functions, sending requests
Obj1=obj;
Create_ajax ();
Http_request.onreadystatechange =processobj; Determine how and when the request is sent and whether the next code is executed synchronously
Http_request.open ("POST", url, True);
Http_request.send (NULL);
}
function Processobj () {
document.getElementById (obj1). innerhtml= "Loading ...";
if (http_request.readystate = = 4) {//Judge object state
if (Http_request.status = = 200) {//information has been successfully returned to start processing information
document.getElementById (obj1). Innerhtml=http_request.responsetext;
} else {//page is not normal
document.getElementById (obj1). innerhtml= "Loading error.";
}
}
}
The above is the main body of Ajax, and then you need to use AJAX pages to include this JavaScript, you can easily invoke:
Copy Code code as follows:
<script type= "Text/javascript" src= "Ajax.js" src= "Ajax.js" ></script>
Welcome.
<div id= "D1" onclick= "Get_ajax" (' ajax/get1.jsp ', ' D1 '); >d1</div>
<div id= "D2" onclick= "Get_ajax" (' ajax/get2.jsp ', ' D2 '); >d2</div>
<div id= "a" >d2</div>
' ajax/get1.jsp ' and ' ajax/get2.jsp ', are two background to get data page, so, a little click D1, GET1.JSP will be loaded into the dynamic, very convenient, of course, this is just a basic function of the model, you can add a lot of functions, we are free to play it.