views:

606

answers:

3

Is there any way to follow a URL in JavaScript without setting the document.location.href?

I have a page that displays a list of objects and each object may have a file download associated with it, which is accessed via a hyperlink. Clicking the link initiates an AJAX request that ultimately leads to a transient file being generated that can be referenced by a unique and temporary URL.

At the moment when the AJAX call completes, it simply sets the document.location.href to the temporary URL and the file download is initiated. Of course this has the side effect of changing the URL in the browser's address bar, so if the page is refreshed the file is downloaded again rather than the object listing page itself getting refreshed. I guess I could set the URL back to what it was before, but that feels a bit hacky.

Incidentally, I'm using the Prototype JavaScript framework.

+3  A: 

you could open a new window with the new url? or try setting an iframe's url to the new url, both should present a file download (the latter being the better option)

scunliffe
Creating a new window and loading that URL in that window is a good idea. I'm pretty sure most browsers will automatically close the popup window once it realizes that it isn't an HTML file.
Kibbee
I've tried that approach in the past actually and unfortunately you get a nasty flash as the popup appears. It sounds like an iframe is the way to go.
John Topley
+2  A: 

You could use a hidden iframe - set the src of that to the file to download.

Greg
+2  A: 

If you're doing all this just to trigger a file download, it sounds like a good application for using a hidden Iframe. Set the SRC of the Iframe instead, so you don't have to mess with the main page.

Diodeus