17

How do i check if the page has pending AJAX or HTTP GET/POST requests? I use javascript and/or python for this checking.

what i wanted to do is execute a script if a page has finished all requests. onload doesn't work for me, if you used firebugs net panel, you would know. onload fires when the page is loaded but there is a possibility that there are still pending request hanging around somewhere.

thank you in advance.

klambiguit
  • 527
  • 1
  • 3
  • 14

8 Answers8

15

figured it out. thanks for the effort guys. just plain and simple javascript.

interValRef = 0;

interValRef = setInterval("checkState();",100)

function checkState(){
    if(document.readyState == 'complete'){
        clearInterval(interValRef);
        myFunc();
    }
}
klambiguit
  • 527
  • 1
  • 3
  • 14
  • all i wanted was to basically check if the page has fully loaded. no pending http get/post ajax request. the code above did the trick. – klambiguit Jul 16 '10 at 09:31
  • 4
    I'm pretty sure there can still be pending HTTP requests after the readyState has become "complete". The `load` event corresponds with that readyState and it simply means the initial request has loaded all its dependencies (as opposed to no more pending requests). You can see this in Chrome's network tab on a page like yahoo.com. The red line is the `load` event but there are often things pending after that. – user2859458 May 25 '16 at 18:38
8

Here is the best way of checking it.

var loadingDone =  document.readyState=="complete" && jQuery.active === 0;

So loadingDone will be true, if Ajax calls are done. If its false then you can add waiting.

user8462556
  • 369
  • 6
  • 13
4

I see you mention you are using Prototype.js. You can track active requests with Prototype by checking the Ajax.activeRequestCount value. You could check this using setTimeout or setInterval to make sure that any requests triggered on page load have completed (if that's what you're looking to do)

robjmills
  • 18,438
  • 15
  • 77
  • 121
  • 1
    this one is good also but only checks AJAX request. how about http get/post requests? (happens w/ an iframe) – klambiguit Jul 19 '10 at 02:28
1

Assuming you are using prototype.js you could keep track with a counter of all your request objects

var ACTIVE_REQUESTS = 0; // GLOBAL

ACTIVE_REQUESTS++
new Ajax.Request('/your/url', {
  onSuccess: function(response) {
    ACTIVE_REQUESTS--;
    // Handle the response content...
  }
}));

console.log("there are " + ACTIVE_REQUESTS + " open AJAX requests pending");
David W. Keith
  • 2,246
  • 17
  • 20
0

You would need to keep track of each XMLHttpRequest and monitor whether it completes or the asynchronous callback is executed.

John Weldon
  • 39,849
  • 11
  • 94
  • 127
0

I think you want to know if the HTML is fully loaded. In this case you can use the dom:loaded event. Here is an example on how to use it with prototype (but there must be a variant for other JS frameworks):

document.observe("dom:loaded", function() {
  // do whatever you want to do
});

This event will fire as soon as the DOM tree is loaded. So even before all the images or external data (including iframe) are loaded.

2ndkauboy
  • 9,302
  • 3
  • 31
  • 65
0

I can confirm that document.readyState can still be 'complete' even if there are pending requests in network tab.

The code I developed for now to check if the page has been loaded completely or not is:

var prevSize= document.body.innerHTML.length;
var id= setInterval(()=>{
    let currSize=document.body.innerHTML.length;
    if(prevSize===currSize){
          clearInterval(id);
          console.log("page loaded successfully");
        }
     else{
          console.log("loading...");
          prevSize=currSize;
        }
}, 3000)

This checks document.body.innerHTML length in every 3s. Not a best solution, but atleast it works.

-2
document.addEventListener("readystatechange", function(event) {
    if (document.readyState === "complete") {
        console.log("complete");
    }
});
Bucket
  • 7,415
  • 9
  • 35
  • 45
Alexey Bulash
  • 369
  • 1
  • 3
  • 5