1

There a some websites (like youtube) which load most of their content after the DOM is ready through javascript. How can I determine, that all of this requests are done, before I inject my code.

window.onload = function() {};

doesn't work here. My entry-point is always before the whole Ajax content is loaded. Is there something like a global promise?

  • 1
    You can't. They might at any time continue to load even more content. – Bergi Dec 19 '17 at 00:01
  • Are you controlling the code on the page that does this? It should be trivial to make a promise that does wait for all the things you want to wait for. – Bergi Dec 19 '17 at 00:01
  • 1
    +1 for interesting question. Although I'm pretty sure you can't do that I'm curious to see interesting hacks – nicholaswmin Dec 19 '17 at 00:02
  • But what about [pace.js](http://github.hubspot.com/pace/docs/welcome/)? They do it someway, don't they? – Felix Traum Dec 19 '17 at 00:15
  • @FelixTraum Have you checked their source? [This](https://github.com/HubSpot/pace/blob/master/pace.coffee#L285-L395) seems relevant. – Bergi Dec 19 '17 at 05:51
  • This *might* be of help: https://stackoverflow.com/questions/5202296/add-a-hook-to-all-ajax-requests-on-a-page. The principle being that you hook/'sniff' on the default `XMLHttpRequest ` and somehow figure out when they end – nicholaswmin Dec 19 '17 at 08:36

1 Answers1

-1

If you need to track ajax calls. Try using Promises and that way you can gurantee when all calls have finished.

Try this: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all

I use it every now and then to wait for multiple ajax calls to finish and then do something after. Hope this helps!!! Cheers

  • This doesn’t work, because I can only use it with my own specified promises. I need all the other promises, which I didn’t setup personally. – Felix Traum Dec 19 '17 at 07:28
  • It would be interesting to know how else this can be done. My assumption was that you control the ajax requests browser is sending. Again this is not relevant. But see if you can make use of this in any way: https://stackoverflow.com/questions/5202296/add-a-hook-to-all-ajax-requests-on-a-page?noredirect=1&lq=1 – Bajinder Budhwar Dec 20 '17 at 02:11