views:

55

answers:

2

Hey all,
I need to dynamically initiate a download with javascript. I have seen how people do this by doing something like

window.open("some url", "Download");

but I need to do it without changing the url of the current page (and not using frames if I can help it, or created and destroying a frame dynamically). Anybody know how to do this?

A: 

You are on the right track, you have to force the browser to request a new "page", even if that page is a file.

You may want to try one of the keywords for the second parameter of window.open() - like _blank - there must be some way to get Chrome to open the new URL...

davisoa
+1  A: 

You don't need window.open(). It's plain ugly and prone to popupblockers (where you have no control over in clients). Just window.location is sufficient if the response header of the requested download URL contains Content-Disposition: attachment. This won't change the current URL in the browser address bar nor the current page, but just pop a Save As dialogue.

E.g.

window.location = 'http://download.winzip.com/winzip145.exe';
BalusC