views:

56

answers:

1

When I run $.get or .load in jQuery, the request seems to follow 302 redirects perfectly fine and gives me ultimate response, which I can use in the callback for $.get or which plugs into the this element for .load.

While I obviously have the original URL since I am in control of the string that's entered as the first argument in $.get or .load (it would also be helpful if I could get this directly in the callback to simplify things), how can access the final (after redirect) URL in the callback?

I suspect that it would be in the XMLHttpRequest object, for which I have found the specification.

+1  A: 
I'm not sure (and Googling hasn't gotten me anywhere), but what about this unorthodox solution? var hiddenIframe = $('').attr({ style: 'display: none;' }).appendTo('body'); hiddenIframe.attr({ src: 'http://www.direct-me.com' }); hiddenIframe.load(function() { alert(hiddenIframe.attr('src')); }); Basically it just makes a hidden iframe, and then goes to your URL, and the callback for load *should* tell you the final URL.

It appears you cannot. You can get the original src, but then you cannot due to security issues.

alex
Is there any reason you're convinced why this is impossible? After all, I understand that you can get the response headers anyway (http://www.w3.org/TR/XMLHttpRequest/#the-getallresponseheaders-method), so it doesn't seem as if the browser prevents you from accessing that information. Also, cross site security prevents you from making an AJAX request to, say http://facebook.com/delete-my-profile, but I don't see why it would be involved here.
Steven Xu
@Steven I'm not convinced, and I hope someone proves me otherwise.
alex
I'm using Rails, which uses the `redirect_to` method to stop script execution and deliver the location header. I redefined the function to send a text string containing a URL or URL segment in XHR cases or if an xhr=1 hidden variable is set (since, for instance, the Form plugin doesn't actually use an XHR request), and to parse the returned content client-side, redirecting if it parses as a URL. Semantically, it may be better just to custom define a header and send the redirect URL there, but this current approach hasn't produced complications and I presume has better compatibility.
Steven Xu