How does AJAX do antying if XMLHttpRequestObject is deleted and / or contains no value, since it is also null?

So, I am reading an AJAX book, and they talk about using an internal function as a way to handle multiple requests. I understand this, but in this bit of code they used, I don’t understand how the XMLHttpRequestObject variable can be used:

 if(XMLHttpRequestObject) { XMLHttpRequestObject.open("GET", dataSource); XMLHttpRequestObject.onreadystatechange = function() { if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) { document.getElementById("targetDiv").innerHTML = XMLHttpRequestObject.responseText; delete XMLHttpRequestObject; XMLHttpRequestObject = null; } } XMLHttpRequestObject.send(null); } 

My first problem is that they remove the XMLHttpRequestObject , and then, after they are supposedly deleted, they set it to zero. Then, after it supposedly retired and set to null, they use XMLHttpRequestObject.send(null); But how does he do anything when XMLHttpRequestObject is deleted and / or contains no value, since it is also null?

+7
javascript ajax
source share
3 answers

My first problem is that they delete the XMLHttpRequestObject, and then, after it was supposedly deleted, they set it to zero.

I don’t know why they do it. Installing for null seems to me sufficient, but perhaps this solves some of the obscure features of the browser.

Then, after it supposedly retired and set to null, they use XMLHttpRequestObject.send (null);

not. deletion occurs inside the event handler - this function of the event handler is not called until the request is completed and the state of the xhr object does not change, for example, due to the server sending the response, or an error that occurs in the message.

In principle, the calling sequence does not match the declaration sequence. Calling sequence:

 XMLHttpRequestObject.open("GET", dataSource); ... //assign event handler so it can be called later on XMLHttpRequestObject.send(null); ... //request send, program continues //separate context here, goes off when the readystate of the xhr changes //due to server response or error: function() { if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) { document.getElementById("targetDiv").innerHTML = XMLHttpRequestObject.responseText; delete XMLHttpRequestObject; XMLHttpRequestObject = null; } } 
+1
source

The onreadystatechange function onreadystatechange not executed immediately after its creation. As the name implies, it is called when a state changes.

In this case, the XMLHttpRequestObject is deleted only after XMLHttpRequestObject.readyState == 4 and XMLHttpRequestObject.status == 200, that is, after the page has been successfully received.

+2
source

It sets up the function to be called upon state change. This is not called immediately, so it does not use the object immediately.

The fact that you can use XmlHttpRequestedObject internally is some kind of freaky trick of the compiler, basically where it writes ugly code to wrap a function and a variable.

0
source

All Articles