Is there any way to extract the request url from an xhr object? I can see the url in firebug via the channel property but you cant query this using javascript.
-
is this for a firefox extension or for a script on a page? If it's on the page, why wouldn't you already have that info? – Jonathan Fingland May 28 '09 at 14:53
-
I have multiple intervals requesting various data, I have some common ajaxStart & ajaxComplete events to process generic events (show/hide progress indicators) I just needed access to the url for an edge case within the ajaxComplete event. – redsquare May 28 '09 at 15:36
6 Answers
With the following hack no wrapper is required:
var xhrProto = XMLHttpRequest.prototype,
origOpen = xhrProto.open;
xhrProto.open = function (method, url) {
this._url = url;
return origOpen.apply(this, arguments);
};
Usage:
var r = new XMLHttpRequest();
r.open('GET', '...', true);
alert(r._url); // opens an alert dialog with '...'

- 15,496
- 7
- 52
- 40
-
Quite handy. Thanks! I ended up with this: `//---Monkey Patch XMLHttpRequest-----` `var xhrProto = XMLHttpRequest.prototype;` `var origXhrOpen = xhrProto.open;` `xhrProto.open = function (v, u) {` ` this.url = u;` ` this.verb = v;` ` return origXhrOpen.apply(this, arguments);` `};` `//---Usage---` `var xhr = new XMLHttpRequest();` `xhr.open('GET', '...', true);` `console.log(xhr.verb+' : '+xhr.url}+' : '+xhr.responseText+' : '+xhr.statusText);` – kEnobus Jun 28 '19 at 20:44
If you are using jQuery, you can make use of the "beforeSend" function in the AJAX request to modify the jqXHR object. I.e.,
$.ajax({
...
url: "http://some/url",
beforeSend: function(jqxhr, settings) { jqxhr.requestURL = "http://some/url"; },
...
});
The jqXHR object passed to the various callbacks will then have that variable, jqXHR.requestURL
, which you can access.

- 337
- 3
- 3
-
5If you do `jqxhr.requestURL = settings.url`, you can set it up globally in an `$.ajaxSetup` call. – Mathias Lykkegaard Lorenzen Jul 09 '15 at 09:08
I hope I'm understanding your problem correctly.
It should be fairly simple to wrap your XHR objects with your own object to allow for this kind of functionality.
Below is an overly simplified example:
// Assumption: GetXHR() returns a new XHR object, cross browser.
function HTTPGet(url, onStartCb, onCompleteCb)
{
var xhr = GetXHR();
// Construct your own xhr object that contains the url and the xhr object itself.
var myXhr = { xhr: xhr, url: url };
xhr.onreadystatechange = function()
{
if (xhr.readyState == 4 && xhr.status == 200)
{
onCompleteCb(myXhr);
}
};
xhr.open("GET", url);
onStartCb(myXhr);
xhr.send(null);
}
I haven't tested this extensively, but it should work and with some modifications (error handling, passing parameters, etc) you should probably be able to turn this example into a fully functional solution.

- 8,985
- 2
- 29
- 27
-
yeah this would be the best approach however I was hoping for a quick win without the need to refactor all the js I am currently working with. Cheers for the reply – redsquare Jun 01 '09 at 13:04
-
If you could provide some of the code you're currently using, it might be possible to figure out an elegant way to add this functionality without having to significantly modify your code. It should be possible to monkey-patch the XHR objects (I haven't tested this, tho) you're returning to your callbacks (are you?). – Lior Cohen Jun 01 '09 at 13:19
According to https://developer.mozilla.org/en/XmlHttpRequest, you can indeed get channel
if you have elevated privileges; however, channel
is non-standard (might not work in other browsers), and indeed the W3C specs at http://www.w3.org/TR/XMLHttpRequest/ do not mention channel
nor any other way to "extract the request URL". I suspect, therefore, that there is no way to do so reasonably across browsers.

- 854,459
- 170
- 1,222
- 1,395
-
getting this information is not a problem for an extension, (as it has privileged access) but is unavailable as far as I'm aware in normal xmlhttprequest usage – Jonathan Fingland May 28 '09 at 15:16
A couple of related properties that could be useful are:
xhr2
XMLHttpRequest.responseURL
- https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/responseURL.
This is not supported by IE.
fetch
If you are using fetch you can use Response.url
- https://developer.mozilla.org/en-US/docs/Web/API/Response/url.
This is not supported by IE.

- 14,970
- 4
- 36
- 58
I also needed this because I was executing a for loop where I did URL requests.
Since usually you have the URL request before you call the open method on the XMLHttpRequest object, you can assign a random property on the xml object (in my case, the property url):
var xml = new XMLHttpRequest();
xml.onreadystatechange = function() {
if ( xml.readyState == 4) {
console.log(xml.url); // equal to originalUrl
}
};
xml.open("GET", originalUrl, true);
xml.url = originalUrl;
xml.send();

- 3,491
- 2
- 26
- 34