views:

39

answers:

6

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.

A: 

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

John Weldon
any suggestions on the HTML GET/POST request? this would happen if an IFRAME is in a page.
juniorSE
A: 

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");
Pullets Forever
i am using prototype.js.. i'll check this out.. thanks.. :)
juniorSE
A: 

i think that will not solve the problem. Even if you got the status of an AJAX request, that's just one request, another http/ajax request might follow. in between those events, the status for an ajax request is completed so the script i would want to execute will be executed w/c is not what i really wanted. what i wanted is to check if there are no pending request.

i thought something like this:

while(1){
    if( window.status == 'Done'){
        executeFunction();
        break;
    } else {
        //set time out for a few milliseconds
    }
}
juniorSE
this is in response to John Weldon's answer, i can't create a block of code if i use the add comment below his answer so i added an answer. :)
juniorSE
window.status always returns an empty string even though the status bar is still busy..
juniorSE
A: 

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();
    }
}
juniorSE
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.
juniorSE
A: 

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.

Kau-Boy
A: 

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)

seengee
this one is good also but only checks AJAX request. how about http get/post requests? (happens w/ an iframe)
juniorSE