Is there an easy way to detect if an XMLHttpRequest
is active in the browser window? Or how many are active? ie. Is there a way to detect if there are any AJAX calls active in my browser window?
Extension of question: Using javascript is there a way I can see if any XMLHttpRequests are open? Such as "window.XMLisActive()"
or something like that?
Solution: Ended up writing a wrapper for XMLHttpRequest
: gist here
To view the request or response HTTP headers in Google Chrome, take the following steps : In Chrome, visit a URL, right click , select Inspect to open the developer tools. Select Network tab. Reload the page, select any HTTP request on the left panel, and the HTTP headers will be displayed on the right panel.
There is absolutely no way to know with certainty if a request came from a browser or something else making an HTTP request. The HTTP protocol allows for the client to set the User Agent arbitrarily.
Inspecting the HTML Response Try right clicking on a piece of information somewhere on the actual web page itself and clicking “Inspect” or “Inspect Element.” This should automatically jump you to the correct spot in the “Elements” tab of the developer tools and show you the exact HTML markup for that element.
HTTP request headers are easily accessible in the web browser itself. To access them, Open the webpage whose headers have to be checked. Right click and select ‘Inspect’ to open developer tools. Select the network tab and refresh or reload the page. Select any HTTP request from the left panel and the header will be displayed on the right.
Nine separate tabs in the HTTP window allow you to store and test multiple messages and switch between them as needed. When the request is defined, press Send to send it to the web server. You can analyze the response immediately in the right-hand pane, which displays the body (shown above) and header info:
The HTTP method of the request (GET, PUT, POST, etc.) and the target URL Nine separate tabs in the HTTP window allow you to store and test multiple messages and switch between them as needed. When the request is defined, press Send to send it to the web server.
This data is bundled into what’s known as the HTTP request header, and it may include information like the source IP address, user’s browser type (like Mozilla, Chrome, etc.), requested URL, and some additional information.
There is not a way to detect an open connection from JS unless you write a wrapper for XmlHttpRequest
(or monkey patch it) that keeps track of opened connections.
Here's kidcapital's monkey patch, not sure if it's perfect, but it's a good start
(function() {
var oldOpen = XMLHttpRequest.prototype.open;
window.openHTTPs = 0;
XMLHttpRequest.prototype.open = function(method, url, async, user, pass) {
window.openHTTPs++;
this.addEventListener("readystatechange", function() {
if(this.readyState == 4) {
window.openHTTPs--;
}
}, false);
oldOpen.call(this, method, url, async, user, pass);
}
})();
Note
This does not handle fetch
or websockets but you could do something similar in those cases.
You can use FireBug for that.
By using jQuery, you can keep track if there are open requests with the global event handlers: .ajaxStart() and .ajaxComplete()
A global variable set on ajaxStart and reset on ajaxComplete should do the job.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With