Close browser popup page after downloading file - javascript

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.

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.

you can do one thing:
after the "file writing code in your servlet or struts action use"
Step 1: call "response.flushBuffer();"
Step 2: call response.sendRedirect("close.htm")
where close.htm is :
<html>
<script>
window.close();
</script>
</html>
Only problem is identifying if the download is complete. I don't think there is a possible way of doing that. Anyway is there any use of keeping the pop-up open untill the download is complete. You can simply close the pop-up once the download is initiated. Using below JavaScript, can't you?
pop_up= window.open("", "PopUpName");
pop_up.document.write('POPUP TEXT');
pop_up.close();

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.

It's Browser functionality, You can change browser setting for that. You shouldn't go for programming . By default If you will download file in firefox it ask for saving, but in crome It's doesn't ask.
You need to change setting of your browser.

Related

window.open() method with IE 8 - Force window to open outside of tabs

I am trying to make a program that is opened by physically clickng on the .HTML file, not through a web-page, and I need it to open up in its own window, NOT the same window in a new tab, without giving warnings.
I can not just change the settings in internet explorer on everyones computer.
So far the ONLY thing I have come up with that will make the window open up without a new tab is to have a secondare html file that uses window.open() to open the main file with the correct parameters.
if I do:
window.open('mainFile.html','_self','width=540, height=880')
it still opens it up in the same window with a new tab, and doesn't resize it.
if I do:
window.open('mainFile.html','','width=540, height=880')
window.close('secondFile.html')
It opens the main one up in a new window, but prompts the user to close the first file. I don't want anyone to know that the second file is not the main file. This ONLY needs to work in IE-8 with default settings.
*For clarification, the window.open() javascript method is called immediately and automatically upon opening the secondary file, so it can open the main file right away. If someone can think of a clever way to acheive the same results without any extra work on the program users parts, I am open to suggestions.
*I know this has to be possible in some way, as another programmer here made a program in the past with javascript, opened the same way my file is, and it does exactly what this one needs to. but unfortunately, they are not available for questioning and the source code is hidden and not accessable.
I GOT IT! Took a ridiculous amount of experimenting, but it works flawlessly now. I feel bad for asking this question now, as it was an extremely simple solution, but for anyone interested, I used a second helper .html file with the lines as follows
<SCRIPT LANGUAGE="JavaScript">
window.open('','_self',''); // this makes the window re-open itself via javascript
window.open('directory/myRealFile','_blank',','width=540,height=880,resizable=yes,scrollbars=yes');
window.close(); // since javascript opened it, it will close without a prompt
</script>

Is it possible not to allow iframe to show file download popup?

I have a code like:
<iframe src="http://www.upnp.org/download/UPNP_understandingUPNP.doc"></iframe>
When this code is rendered, browser file download prompt is shown to user. I'd want to prohibit iframe from showing this download popup.
Is it possible?
I'd want it as iframe's url is controlled by users, not site owner.
No.
The server hosting the URL can decide if it wants to mark it as an attachment (suggesting that the browser to save it) or inline (suggesting that the browser should open it; using a plugin if one is available).
The browser decides how to handle it.
The page linking to it (even if it does so via an iframe) has no control over any of the above.
To avoid the download popup, you could use ajax instead of an iframe to load the url.

Open url in new tab using javascript without click an anchor

i read a lot of solutions but all of them are clicking a url, and it works, but my client ask me to do is users opens his website, it automatically open a new tab with some special offers , so my question, is there any way to open a new tab without any user intervention? , maybe a jquery plugin?, i know the tabs rely on the web browser, but it have to be a way, a lot of web pages does it,but how?
Greetings
Without modifying the configuration of browser, the answer is no, only trusted event can open a new window (or tab)
You may ask if your client could change their browser's configuration to allow a popup window from an untrusted javascript program.
Write script in your page to open new page:
<script>
window.open("specialOffers.aspx");
</script>
and in the "specialOffers.aspx" page in the tag write:
<base target="_blank"/>

Is it possible to send an html file with a downloadable file in one response?

Basically, I am using an iframe to download a file. I set the source of the iframe to the file I want to download. That problem isn't that I can't get a download dialog. The problem is I can't close the window after the download is started.
What I was thinking is I could send back one file with this header.
"Content-Disposition","attachment;filename=test.txt"
Then I would like to send back another HTML for the browser to display. The whole point of the second file is to close the browse window.
BTW: It is far to late to go away from popups.
No, you can't send both a file and a page in the same response.
If you want two responses, you have to send two requests.
Alternatively, you can start the download, then use a timeout to close the window after the download has started:
window.location.href = 'test.txt';
window.setTimeout(function(){
window.close();
}, 100);
The whole point of the second file is to close the browse window.
You don't need this. Just setting that header on the response with the to-be-downloaded file is more than sufficient to get a Save As dialogue. You don't need to open the link in a new/blank window. Just let the link/form point to the desired URL and it'll open the Save As dialogue. The currently opened page will remain the same.

In javascript, how can I detect when the browser brings up the open/save/cancel prompt when loading a file?

In javascript, how can I detect when the browser brings up the open/save/cancel prompt when loading a file?
If possible, is it the same for all browsers?
I'm trying to fix a problem with users clicking a download button, being impatient and clicking it again, and again which is taxing our servers. I tried just hiding the button while the file is processed, but the icon will come back before the open/save/cancel prompt shows up.
The code goes something like this:
...
users clicks button,
button hides,
external process gets the file,
button appears ...
You can't.
How the browser deals with a requested resource - open it, start an external application, offer a "save" dialog - is outside of what you can control using JavaScript.
You can't! No way, that's it!

Categories

Resources