AJAX with memory _ajax related

Source: Internet
Author: User
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.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.