views:

142

answers:

2

When user clicks on a hyperlink, i am opening a popup window which downloads a file. Post download, it shows open/save dialog box. I want to close the popup window once the download is done (when user is prompted for saving the file). I have tried window.close method, but it doesnt work as the context is not the popup window but the open/save dialog box.

+2  A: 

I think you can not control it programatically. This is browser-specific thing where some browsers allow you to tick on a check box to close the window and so on.

Sarfraz
Yes. Some users like to have that window stick around, eg. so they can run ‘Run’ from it after downloading; that how they prefer it and you shouldn't try to interfere with that (not that you can). Everyone else who doesn't like that behaviour will untick the box to make it go away.
bobince
A: 

Browsers have a habit of deciding for themselves whether to try to download a file or open it inside the browser window, depending on the browser used, plug-ins and server settings. It sounds like you might be opening the link in a new window, as if the browser was going to open the file rather than download it, and then the browser has opted for the download. This leaves the user with a downloaded file and a blank window that you have no control over.

To force it to download, you should be able to set the Content-Type header for the target of the link to application/force-download. How you do this will depend on your setup, and whether the file is downloaded directly (in which case it will be a server setting) or via PHP or .Net (in which case it's easy to programmatically set the header). Also make sure that the hyperlink doesn't have a target="_blank" attribute that opens the link in a new window.

Karl B
Thanks Karl. The problem is not downloading the file but closing the popup window after download.
Ankit
With the correct Content-Type header, and no target="_blank" (or window.open) then the popup shouldn't even open in the first place.
Karl B
I want to open the popup and show a custom message in that window. But once the download is complete, i want that window to close.
Ankit
I think you're going to be out of luck then - I don't believe it's possible to detect when the download is completed. Note that the prompt to save the file is not the point that the download's finished but the point that it starts.If you want to close the popup straight after the user clicks the link then there are a number of ways, but putting onclick="setTimeout(function(){window.close();},1" (ensuring the close happens after the href is fired, not before) would probably do it.
Karl B
Thanks for the response but the download might take time which i can not define. I tried registering the script to close window but it doesnt get fired post download however it fires if i remove the logic of downloading the file
Ankit
The code I gave you shouuld close the window as soon as the user started the download, unless there's already an onclick handler. As I said: you'll never know how long the download will take.
Karl B