By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,818 Members | 2,121 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,818 IT Pros & Developers. It's quick & easy.

Ajax and IE6

P: n/a
Hi all,

I've seen a lot of ajax examples on the web that used an event handler
called 'processChange' for the onreadystatechange event. Normally,
this looks like this:

if(window.XMLHttpRequest) obj = new XMLHttpRequest();
else if(window.ActiveXObject) obj = new
ActiveXObject('Microsoft.XMLHTTP');

obj.onreadystatechange = processChange;
....

function processChange(){
if(obj.readystate != 4) return;
if(obj.status == 200){
// do something
}
}

Well, this works on IE6 and Firefox, but I would like to use this
instead of obj inside of the processChange function (this.readystate
and this.status). This works fine on firefox, but on IE6 it doesn't.
Can someone tell me why?

For the record, I want to use this instead of obj because I want to
make a series of request with a for loop and since happens all inside
of the same closure, obj would refer to the last instance of my
xmlhttprequest.

Thanks,

Vincent
Jul 21 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Jul 22, 3:41*am, Vincent <vincentdeh...@gmail.comwrote:
if(window.XMLHttpRequest) obj = new XMLHttpRequest();
else if(window.ActiveXObject) obj = new
ActiveXObject('Microsoft.XMLHTTP');

obj.onreadystatechange = processChange;
...

function processChange(){
* *if(obj.readystate != 4) return;
* *if(obj.status == 200){
* *// do something
* *}

}
<snip>
For the record, I want to use this instead of obj because I want to
make a series of request with a for loop and since happens all inside
of the same closure, obj would refer to the last instance of my
xmlhttprequest.
Rather than using 'this', which changes meaning depending on the
context where the code gets executed, there is another trick you can
use to avoid creating a closure: pass the value of obj instead of
using obj directly.

/* function generator allowing us to pass
* the xmlhttprequest object rather than
* create a closure:
*/
function makeProcessChangeHandler (ajaxObj) {
/* admittedly, it is still a closure
* only in a different scope
*/
return function () {
if(ajaxObj.readystate != 4) return;
if(ajaxObj.status == 200){
// do something
}
}
}
obj.onreadystatechange = makeProcessChangeHandler(obj);
Jul 22 '08 #2

P: n/a
On Jul 22, 12:40 pm, slebetman wrote:
<snip>
... , there is another trick you can use to avoid
creating a closure: pass the value of obj instead of
using obj directly.

/* function generator allowing us to pass
* the xmlhttprequest object rather than
* create a closure:
*/
function makeProcessChangeHandler (ajaxObj) {
/* admittedly, it is still a closure
* only in a different scope
*/
return function () {
if(ajaxObj.readystate != 4) return;
if(ajaxObj.status == 200){
// do something
}
}}

obj.onreadystatechange = makeProcessChangeHandler(obj);
That is creating a closure.
Jul 22 '08 #3

P: n/a
On Jul 22, 1:40*pm, slebetman <slebet...@gmail.comwrote:
>
Rather than using 'this', which changes meaning depending on the
context where the code gets executed, there is another trick you can
use to avoid creating a closure: pass the value of obj instead of
using obj directly.

/* function generator allowing us to pass
** the xmlhttprequest object rather than
** create a closure:
**/
function makeProcessChangeHandler (ajaxObj) {
* /* admittedly, it is still a closure
* ** only in a different scope
* **/
* return function () {
* * if(ajaxObj.readystate != 4) return;
* * if(ajaxObj.status == 200){
* * // do something
* * }
* }}

obj.onreadystatechange = makeProcessChangeHandler(obj);
Yeah !
Other objects and data could be captured as needed in that same
closure as well :

xhr.onreadystatechange = (function (xhr, object, data) {
return function () {

//xhr, object, and data have been captured in the closure.

}
})(xhr, someOtherObject, i);
"auto-reposting, closurized, xhr monitor and exerciser"
http://tinyurl.com/6mm5tm :-)

--Jorge.
Jul 22 '08 #4

P: n/a
On Jul 22, 5:40*pm, Jorge <jo...@jorgechamorro.comwrote:
>
"auto-reposting, closurized, xhr monitor and exerciser"
http://tinyurl.com/6mm5tm :-)
>
BTW, FF3 now seems to handle 6 simultaneous requests...
Safari, Opera: 4
FF2: 2
etc: 2

--Jorge.
Jul 22 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.