2

I want to check the active Ajax requests from a site which is not using jQuery so How can I write the equivalent of jQuery.active?

I want to check if the ajax requests finished after visiting the page.

i.e. I found window.XMLHttpRequest class but not sure how to use it. Any help appreciated.

Mesut GUNES
  • 7,089
  • 2
  • 32
  • 49
  • Check http://stackoverflow.com/a/3148506/2025923. It's just a variable defined on jQuery object. – Tushar Nov 10 '15 at 08:19
  • 1
    @Tushar thanks but every solutions used jQuery. – Mesut GUNES Nov 10 '15 at 08:21
  • 1
    This doesn't answer the question but to clarify ... `window.XMLHttpRequest.DONE` is one of a family of read-only constants *(not quantities)*, each representing one of the possible states of an XHR instance - UNSENT:0; OPENED:1; HEADERS_RECEIVED:2; LOADING:3; DONE:4 . Not too sure where this is specified (W3C? IANA?) but it is common (IE, Chrome, Opera), probably universal. – Roamer-1888 Nov 10 '15 at 13:40
  • @Roamer-1888 which of the following options shows the active XHR : UNSENT:0; OPENED:1; HEADERS_RECEIVED:2; LOADING:3; DONE:4 ? – Mesut GUNES Nov 10 '15 at 13:42
  • As I say, they are constants. – Roamer-1888 Nov 10 '15 at 13:43
  • And there's no such thing as "the active XHR". There may be none, one or more than one XHR at any given point in time and each of them will be in one of those states. – Roamer-1888 Nov 10 '15 at 13:46
  • @Roamer-1888 updated the question. I wanna check if the ajax finishes or not after visit. – Mesut GUNES Nov 10 '15 at 13:52
  • Assuming this to be a Greasemonkey scenario, [this question](http://stackoverflow.com/questions/629671/how-can-i-intercept-xmlhttprequests-from-a-greasemonkey-script) may possibly provide part of the solution. – Roamer-1888 Nov 10 '15 at 14:20
  • @MesutGÜNEŞ did you ever figure this out? – John Pollard Mar 28 '17 at 14:30
  • @JohnPollard unfortunately I still don't know the answer. – Mesut GUNES Mar 28 '17 at 15:29

1 Answers1

1

I ended using this. If window.ajax_active == 4 then I know the ajax request has completed. Seems to work for me so far.

window.ajax_active = 0;

(function(open) {
  return XMLHttpRequest.prototype.open = function(method, url, async, user, pass) {
    this.addEventListener('readystatechange', (function() {
     if (this.readyState == 1) {
        window.ajax_active += 1;
     } else if (this.readyState == 4){
        window.ajax_active -= 1;
     } 
    }), false);
    return open.call(this, method, url, async, user, pass);
  };
})(XMLHttpRequest.prototype.open);
John Pollard
  • 3,729
  • 3
  • 24
  • 50