开发者

Is there a way to use JavaScript to check to see if any AJAX calls are currently awaiting a response?

开发者 https://www.devze.com 2023-01-26 09:07 出处:网络
I\'m doing some YouTube rela开发者_开发百科ted stuff in a UIWebView on iOS. I\'m using the mobile version of YouTube and they load things in the absolute strangest way (they make a bunch of AJAX calls

I'm doing some YouTube rela开发者_开发百科ted stuff in a UIWebView on iOS. I'm using the mobile version of YouTube and they load things in the absolute strangest way (they make a bunch of AJAX calls to actually load the page while only loading basically a template first).

Something else strange: I'm logging the entire body of the page with this:

NSLog([webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.innerHTML"]);

What's strange is that the logging doesn't happen until all AJAX is finished and the page is loaded. But I'd always thought that JavaScript doesn't wait for AJAX to finish before proceeding to execute the next line of code - this is contradicting that; it's actually waiting for AJAX to complete then returning the true, complete HTML of the page.

Any help? I'm confused.


XMLHttpRequests aren't necessarily asynchronous.


If we assume your AJAX is asynchronous, the page load order could affect what you're seeing.

  1. Script tags in are executed first. The body is not available.
  2. Script placed between are executed as they are encountered during page load, body is available up until the point the script is.
  3. When all resources are loaded window.onload event is executed.

So depending on where your AJAX calls and where your logging call is this could affect the order you are seeing things.

Then depending on the finer implementation details of NSLog(), you may not see the output from a logging statement the very instant you are calling it. Many times there are buffers which are flushed now and then - perhaps only when the UI thread is not busy doing anything else.

0

精彩评论

暂无评论...
验证码 换一张
取 消