Forum: Rails Spinoffs (closed, excessive spam) Aborting Ajax request

1db9dd106bf8f5330dce1f7ec0a397f6?d=identicon&s=25 Satoru Moriwaki (Guest)
on 2008-06-19 10:48
(Received via mailing list)
Hi,

Does anyone know why Prototype invokes the Ajax.Responders onComplete
method on IE and FireFox but not on Safari when Ajax.Request is
aborted by invoking the transport.abort() method? (Is it a bug for
Safari not implementing the XMLHttpRequest object correctly?)

If I invoke the transport.abort(), it screws up the
Ajax.activeRequestCount on Safari since Ajax.Responders onComplete is
not invoked.

Please let me know if there is a work around for this or if there is a
better way of aborting Ajax request.
2a41d6b08b5d05cf4f483f21ecd7dec1?d=identicon&s=25 Irfan, Ghouseuddin Syed (Guest)
on 2008-06-19 11:43
(Received via mailing list)
This happens because onTimeout never fires on Safari which updates the
activeRequestCount.

Instead of invoking abort on transport directly you can extend the
Ajax.Request class with the following method. It takes care of calling
onTimeout for Safari thus updating the activeRequestCount.

Ajax.Request.prototype.abortRequest = function() {
  this.aborted = true;
  this.transport.abort();
  if(/Konqueror|Safari|KHTML/.test(navigator.userAgent)){
    (this.options['onTimeout'] || Prototype.emptyFunction)();
  }
}

Just paste the code anywhere after your prototype.js include.

Usage:

var myReq = new Ajax.Request(blah..blah..);
myReq.abortRequest();
1db9dd106bf8f5330dce1f7ec0a397f6?d=identicon&s=25 Satoru Moriwaki (Guest)
on 2008-06-19 12:00
(Received via mailing list)
Thanks for the tip.  I tried it right away, but it didn't work (do I
have to implement onTimeout callback to decrement
Ajax.activeRequestCount?)

I did the following and it seems working fine.

...
...
...
this._request.transport.abort();

// for Opera and Safari
if(Prototype.Browser.Opera || Prototype.Browser.WebKit)
{
  Ajax.activeRequestCount--;
}

On Jun 19, 6:43 pm, "Irfan, Ghouseuddin Syed" <is...@corp.untd.com>
2a41d6b08b5d05cf4f483f21ecd7dec1?d=identicon&s=25 Irfan, Ghouseuddin Syed (Guest)
on 2008-06-19 12:17
(Received via mailing list)
Yes that should work.

You can even try the following:

Ajax.Request.prototype.abortRequest = function() {
  this.transport.abort();
  if(/Konqueror|Safari|KHTML/.test(navigator.userAgent)){
  Ajax.activeRequestCount--;
  }
}

or

Ajax.Request.prototype.abortRequest = function() {
  this.transport.abort();
  if(Prototype.Browser.Opera || Prototype.Browser.WebKit){
Ajax.activeRequestCount--;
  }
}
1db9dd106bf8f5330dce1f7ec0a397f6?d=identicon&s=25 Satoru Moriwaki (Guest)
on 2008-06-19 12:37
(Received via mailing list)
Thanks for the another tip.

I'm wondering why Prototypejs doesn't officially support the abort()
method.... I think a lot of people need this right?

But anyways, thanks again.

Satoru


On Jun 19, 7:16 pm, "Irfan, Ghouseuddin Syed" <is...@corp.untd.com>
This topic is locked and can not be replied to.