T
Thomas 'PointedEars' Lahn
Hello all,
many of the subscribers of this newsgroup know through experience that
Wikipedia is not exactly a fountain of wisdom when it comes to ECMAScript-
based programming. In particular, the last paragraph of
<http://en.wikipedia.org/wiki/XMLHttpRequest#The_onreadystatechange_event_listener>,
which might have led to <<[email protected]>, left me puzzled:
| The onreadystatechange event listener
|
| If the open method of the XMLHttpRequest object was invoked with the third
| parameter set to true for an asynchronous request, the onreadystatechange
| event listener will be automatically invoked for each of the following
| actions that change the readyState property of the XMLHttpRequest object.
|
| Theoretically, state changes should work like this:
|
| - After the open method has been invoked successfully, the readyState
| property of the XMLHttpRequest object should be assigned a value of 1.
| - After the send method has been invoked and the HTTP response headers
| have been received, the readyState property of the XMLHttpRequest object
| should be assigned a value of 2.
| - Once the HTTP response content begins to load, the readyState property
| of the XMLHttpRequest object should be assigned a value of 3.
| - Once the HTTP response content has finished loading, the readyState
| property of the XMLHttpRequest object should be assigned a value of 4.
|
| However, the major user agents are inconsistent with the handling of the
| onreadystatechange event listener. For example, the following should
| produce four alerts: 1,2,3,4
|
| xmlhttp.open("GET","somepage.xml",true);
| xmlhttp.send(null);
| xmlhttp.onreadystatechange = function() {
| alert(xmlhttp.readyState);
| };
|
| In fact, some browser versions may omit state changes #1 and/or #2; if the
| order of the first two lines is reversed, some browsers may cause state
| change #1 to fire twice (or continue to omit event #2).[26] Due to the
| already-stated inconsistencies, it is bad coding practice to execute the
| xmlhttp's send method before assigning a callback function to
| onreadystatechange event handler. If reversed, that is if one assigned a
| callback function to the onreadystatechange event handler before executing
| the xmlhttp's send method - not after - like one should, the above
| mentioned inconsistencies are no longer issues.[27]
Now is it just me or is this last paragraph just pointless babble, driven by
mythical incantations rather than solid knowledge (who is this Eric
Pascarello person anyway)?
I have never encountered any problem in http.js (formerly: httprequest.js)
or elsewhere using the logical order,
xmlhttp.open("GET", "somepage.xml", true);
xmlhttp.onreadystatechange = function() {
window.alert(xmlhttp.readyState);
};
xmlhttp.send(null);
for when doing it differently, inconsistent behavior and a race condition is
to be expected (it is an *asynchronous* call). And IIUC, that is also what
the last sentence of this paragraph says. Why put error-prone example code
there, then going at length to explain the problems that must be caused by
it, instead of simply putting the correct version and be done with it?
What do you think? Am I missing something important here?
PointedEars
many of the subscribers of this newsgroup know through experience that
Wikipedia is not exactly a fountain of wisdom when it comes to ECMAScript-
based programming. In particular, the last paragraph of
<http://en.wikipedia.org/wiki/XMLHttpRequest#The_onreadystatechange_event_listener>,
which might have led to <<[email protected]>, left me puzzled:
| The onreadystatechange event listener
|
| If the open method of the XMLHttpRequest object was invoked with the third
| parameter set to true for an asynchronous request, the onreadystatechange
| event listener will be automatically invoked for each of the following
| actions that change the readyState property of the XMLHttpRequest object.
|
| Theoretically, state changes should work like this:
|
| - After the open method has been invoked successfully, the readyState
| property of the XMLHttpRequest object should be assigned a value of 1.
| - After the send method has been invoked and the HTTP response headers
| have been received, the readyState property of the XMLHttpRequest object
| should be assigned a value of 2.
| - Once the HTTP response content begins to load, the readyState property
| of the XMLHttpRequest object should be assigned a value of 3.
| - Once the HTTP response content has finished loading, the readyState
| property of the XMLHttpRequest object should be assigned a value of 4.
|
| However, the major user agents are inconsistent with the handling of the
| onreadystatechange event listener. For example, the following should
| produce four alerts: 1,2,3,4
|
| xmlhttp.open("GET","somepage.xml",true);
| xmlhttp.send(null);
| xmlhttp.onreadystatechange = function() {
| alert(xmlhttp.readyState);
| };
|
| In fact, some browser versions may omit state changes #1 and/or #2; if the
| order of the first two lines is reversed, some browsers may cause state
| change #1 to fire twice (or continue to omit event #2).[26] Due to the
| already-stated inconsistencies, it is bad coding practice to execute the
| xmlhttp's send method before assigning a callback function to
| onreadystatechange event handler. If reversed, that is if one assigned a
| callback function to the onreadystatechange event handler before executing
| the xmlhttp's send method - not after - like one should, the above
| mentioned inconsistencies are no longer issues.[27]
Now is it just me or is this last paragraph just pointless babble, driven by
mythical incantations rather than solid knowledge (who is this Eric
Pascarello person anyway)?
I have never encountered any problem in http.js (formerly: httprequest.js)
or elsewhere using the logical order,
xmlhttp.open("GET", "somepage.xml", true);
xmlhttp.onreadystatechange = function() {
window.alert(xmlhttp.readyState);
};
xmlhttp.send(null);
for when doing it differently, inconsistent behavior and a race condition is
to be expected (it is an *asynchronous* call). And IIUC, that is also what
the last sentence of this paragraph says. Why put error-prone example code
there, then going at length to explain the problems that must be caused by
it, instead of simply putting the correct version and be done with it?
What do you think? Am I missing something important here?
PointedEars