1

When we use $.ajax object it is easy to use .ajaxComplete() to detect ajax request from any handler of document.

Is what we are working with javaScript only, no jQuery, how can we achieve same result like .ajaxComplete().

cweiske
  • 30,033
  • 14
  • 133
  • 194
Vipul Hadiya
  • 882
  • 1
  • 11
  • 22
  • please refer: [how to make an ajax call without jquery](http://stackoverflow.com/questions/8567114/how-to-make-an-ajax-call-without-jquery) – Manish Hada Jan 18 '16 at 19:58

1 Answers1

0

You could do what jQuery did and create your own wrapper around XHR/ActiveX. Make sure you use it exclusively in your application and then you can have it do whatever you want (including calling back registered functions) whenever it completes a request.

Edit: If you want to capture ANY xhr completion, check out this fiddle I made: https://jsfiddle.net/0bjfLey9/1. Basically you wrap the send() function and then wrap the onreadystatechange handler, inserting whatever logic you want into the wrapper.

Cameron
  • 434
  • 2
  • 4
  • I need a function which can detect any XMLHttpRequest fron the document. – Vipul Hadiya Jan 19 '16 at 14:12
  • jQuery does what Cameron is suggesting. Any ajax-related function of jQuery ($.post, $.get, $.ajax, $.load,...) uses the same function internally that handles the XHR/ActiveX object. It is this function that will trigger the .ajaxComplete event. You will have to do the same in your application. – Ward D.S. Jan 19 '16 at 15:15
  • @WardD.S. Not necessarily. Take a look at the fiddle I posted and you'll see a potential solution (hack) that satisfies Vipul's need. By targeting the XHR prototype you can affect the behavior of **all** XHR requests, including those in external libraries. – Cameron Jan 19 '16 at 15:23
  • It's a smart solution but I doubt that should be common practice :) If he had a specific need (e.g. debugging 3rd party software that he can't control) this would be great! Just not the ideal solution for a beginner, worth it to include and illustrate but also discourage... Again though, smart trick storing and calling the original function after running some of your own code. – Ward D.S. Jan 19 '16 at 15:37
  • So this means even if 3rd party code does not used jQuery for making the Ajax calls `.ajaxComplete()` will not even catch it right? I guess i need to listen to dom changes. Modern APIs for that are not expensive I think. – redanimalwar Oct 21 '19 at 13:25