Window. activexobject: return a bool value to check whether the browser supports ActiveX controls.
3 // obtain the XMLHTTPRequest object for different browsers
4 function createxmlhttprequest ()
5 {
6 if (window. activexobject)
7 {
8 XMLHTTP = new activexobject ("Microsoft. XMLHTTP ");
9}
10 else
11 {
12 XMLHTTP = new xmlhttpresquest ();
13}
14}
16 // click the JS function called when btnsayhello
17 function btnsayhello_onclick ()
18 {
19 createxmlhttprequest ();
20 XMLHTTP. onreadystatechange = handlestatechange;
21 XMLHTTP. Open ("Post", "output. aspx", true );
22 XMLHTTP. Send (null );
23}
24
25 // callback function
26 function handlestatechange ()
27 {
28 If (XMLHTTP. readystate = 4)
29 {
30 if (XMLHTTP. Status = 200)
31 {
32 document. getelementbyid ("result"). innerhtml = XMLHTTP. responsetext;
33}
34}
35}
Next I will give a brief explanation of this Code. First, XMLHTTP is a global variable. After initialization, it is responsible for the core of the entire Ajax application. The createxmlhttprequest method is used to obtain an instance of the XMLHTTPRequest object. We note that because different browsers have different methods to obtain objects, we need to first check the browser type and then use the corresponding method to obtain objects, which is 10 minutes of trouble. Btnsayhello_onclick is a core function that is called when a button is clicked. It first initializes XMLHTTP, then specifies the callback function as handlestatechange, and finally asynchronously releases the POST request to call the output. ASPX page. Finally, handlestatechange is called automatically when the Request status of XMLHTTP changes. In this function, when the status is normal, set the content of the DIV with the ID of "result" as the call result. That is, the string output by output. aspx.