Easy Tutorial
❮ Java Print The Triangle Verilog Basic Syntax ❯

Understanding the onreadystatechange Property

Category Programming Technology

When writing Ajax methods, we often write code similar to this:

Ajax Code:

var xmlHttp;
// Create an XmlHttpRequest object
function createXMLHttpRequest(){
    if(window.ActiveXObject){
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else if(window.XMLHttpRequest){
        xmlHttp = new XMLHttpRequest();
    }
}
// Start a request
function startRequest(){
    createXMLHttpRequest();
    xmlHttp.onreadystatechange = handlestatechange;
    xmlHttp.open("GET", "SimpleRespose.xml", true);
    xmlHttp.send(null);
}

function handlestatechange(){
    if(xmlHttp.readyState == 4){ // Describes a "loaded" state; at this point, the response has been fully received.
        if(xmlHttp.status == 200){ // 200 indicates successful receipt
            alert("The Server Replied with:" + xmlHttp.responseText);
        }
    }
}

When I first read this code, I felt something was off, but I couldn't pinpoint what. As I delved deeper into Ajax code, this feeling persisted. Later, I understood what was causing this feeling.

Look at the startRequest function. We see that xmlHttp.onreadystatechange points to a function that is triggered when xmlHttpRequest.readyState changes. Let's imagine the steps of the entire request process. Now we click a button, triggering the startRequest function. The function proceeds, the first step being createXmlHttpRequest(), which creates an xmlHttpRequest object. When it completes, xmlHttpRequest.readyState is 0 (tracked with window.alert), and the program continues. xmlHttp.onreadystatechange = handlestatechange, but since the state hasn't changed (xmlHttpRequest.readyState is 0), the function isn't triggered. Next are open() and send(), so the entire function should not have triggered the handlestatechange function, yet the result is correct.

Later, I tracked the changes in xmlHttp.readyState with window.alert and found that the mechanism is as follows. After creating an xmlHttpRequest object, xmlHttp.readyState is 0, and then xmlHttp.onreadystatechange doesn't run. Next is open(), after which xmlHttp.readyState is 1, causing a breakpoint at the open() function, preserving the context, and then returning to run xmlHttp.onreadystatechange, followed by the send() function, after which xmlHttp.readyState is 2, and it returns to run xmlHttp.onreadystatechange again. This continues.

Browsers, unable to truly program in an object-oriented way, have found a compromise, but this method seems awkward. After much thought and discussion with a classmate, we came to this conclusion.

onreadystatechange: Set to point to the handlestatechange function (somewhat difficult to understand)

A function is a subroutine that performs a specific task. After compilation, its executable code is allocated in the code segment, while its parameters and variables are in the stack segment. Therefore, when the main program calls a function, it actually transfers the program execution address to the entry address of the function in the code segment, executing it. Each function has a definite entry address in the code segment, and the program executes accordingly. When a return instruction is encountered (indicating the end of the program), the program returns to the breakpoint of the caller of the function and continues execution. Since functions have a definite entry address (the function name actually represents its entry address), pointers can point to them, and these pointers are also known as function pointers.

Original Source: http://blog.chinaunix.net/uid-20730110-id-1883890.html

** Click to Share Notes

Cancel

-

-

-

❮ Java Print The Triangle Verilog Basic Syntax ❯