tags:

views:

168

answers:

2

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.

A: 

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.

Alex Martelli
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
+1  A: 

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.

Lior Cohen
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
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