I need to close the browser window after a force-download

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
intergroove
Forum Commoner
Posts: 32
Joined: Wed Oct 11, 2006 4:42 pm

I need to close the browser window after a force-download

Post by intergroove »

I am scripting a download service, as the website is on one server and the download files are on another I am using a pop-up window to launch a php script. The script produces a force-download. All of this works fine - but I'd like to close the window.

Here is my force download:

Code: Select all

header("Pragma: public"); 
        header("Expires: 0"); 
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
        header("Cache-Control: private",false); 
        header("Content-Type: $ctype"); 
        header("Content-Disposition: attachment; filename=\"".basename($file)."\";"); 
        header("Content-Transfer-Encoding: binary"); 
        header("Content-Length: ".@filesize($file)); 
        set_time_limit(0); 
	@readfile($file) or die("File not found.");
How would I go about adding a javascipt element to the code to enforce the close?

Any help greatly appreciated.
calevans
Forum Newbie
Posts: 4
Joined: Tue Jun 06, 2006 3:59 pm
Location: Nashville, TN

Re: I need to close the browser window after a force-downloa

Post by calevans »

intergroove wrote:I am scripting a download service, as the website is on one server and the download files are on another I am using a pop-up window to launch a php script. The script produces a force-download. All of this works fine - but I'd like to close the window.

Here is my force download:

Code: Select all

header("Pragma: public"); 
        header("Expires: 0"); 
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
        header("Cache-Control: private",false); 
        header("Content-Type: $ctype"); 
        header("Content-Disposition: attachment; filename="".basename($file)."";"); 
        header("Content-Transfer-Encoding: binary"); 
        header("Content-Length: ".@filesize($file)); 
        set_time_limit(0); 
	@readfile($file) or die("File not found.");
How would I go about adding a JavaScript element to the code to enforce the close?

Any help greatly appreciated.
If I'm not mistaken, you simply add:

<html>
<body>
<script>
document.close(); // possibly window.close();
</script>
</body>
</html>

I could be wrong though. This of course will close the window whether the file downloads ot not since it does not honor the die() in PHP. So even if this works, it may not be the optimal solution.

=C=
intergroove
Forum Commoner
Posts: 32
Joined: Wed Oct 11, 2006 4:42 pm

Post by intergroove »

I tried that method, but HTML wasn't executed as the headers where pushed out first (this what I think is happening - bit ofa novice in this area).
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

intergroove wrote:as the website is on one server and the download files are on another I am using a pop-up window to launch a php script. The script produces a force-download.
I don't understand. Why exactly is it you have a pop-up?
intergroove
Forum Commoner
Posts: 32
Joined: Wed Oct 11, 2006 4:42 pm

SOLUTION

Post by intergroove »

I have come up with a solution:

Send the launch script to an invisible IFRAME. Then I don't have to think about closing the window. Eureka!

So I use the following HTML:

Code: Select all

<iframe src="blank.htm" name="dloader" 
id="dloader" width="0" height="0" border="0" 
frameborder="0" scrolling=no marginwidth="0" 
marginheight="0" vspace="0" 
hspace="0"></iframe>
where blank.htm is an empty html file (just: <html><header></header><body></body></html> )

Now my download link launches the php script into 'dloader'

If anyone knows of any just impedement (ie will this work on all browsers) then let me know asap.


chrs
intergroove
Forum Commoner
Posts: 32
Joined: Wed Oct 11, 2006 4:42 pm

Post by intergroove »

volka wrote:
intergroove wrote:as the website is on one server and the download files are on another I am using a pop-up window to launch a php script. The script produces a force-download.
I don't understand. Why exactly is it you have a pop-up?
Non-lateral thinking. Designed this early in the project - have just been piecing everything together when I realised the pop-up hangs around (I was just happy toget the force-download to work when I first scripted it). The IFrame solution seems good at the moment though.

Any other suggestions/opinions gratefully received.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

intergroove wrote:Send the launch script to an invisible IFRAME. Then I don't have to think about closing the window. Eureka!
Forgive me for beeing dumb but why do you need "another" target window/frame at all? This is no rethorical question; I really do not understand what you're trying to achieve with it. I take it you have some sort of link to download something, simple example

Code: Select all

<a href="download.php?id=4711">
but for some reason you seem to have

Code: Select all

<a href="download.php?id=4711" target="xyz">
But why?
intergroove
Forum Commoner
Posts: 32
Joined: Wed Oct 11, 2006 4:42 pm

Post by intergroove »

volka wrote:
intergroove wrote:Send the launch script to an invisible IFRAME. Then I don't have to think about closing the window. Eureka!
Forgive me for beeing dumb but why do you need "another" target window/frame at all? This is no rethorical question; I really do not understand what you're trying to achieve with it. I take it you have some sort of link to download something, simple example

Code: Select all

<a href="download.php?id=4711">
but for some reason you seem to have

Code: Select all

<a href="download.php?id=4711" target="xyz">
But why?
My download files are stored on a different server below public level. I need to keep the original page in place while force-downloading a file.

The force-download script is on the file server. It needs to be there to get to the files below the root.

If I link directly to it my page refreshes to the blank download page.

Does that make sense? I guess being a lone developer I'm going to make incorrect assumptions in places and use crazy unessessary work arounds. But I try my best.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

This is not a php issue - you have to do this in the client.

I would use a javascript function to trigger the page load in a new window (the forced download) then self.close() the current window.

Cheers,
Kieran
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

intergroove wrote:If I link directly to it my page refreshes to the blank download page.
Take a look at http://httpd.apache.org/download.cgi
There's a section "Apache HTTP Server x.y.z is the best available version" and a link e.g. to the unix sources. The link points to a mirror, not an apache.org server. Do you get a new,blank windows when clicking the link?
intergroove
Forum Commoner
Posts: 32
Joined: Wed Oct 11, 2006 4:42 pm

Post by intergroove »

Kieran Huggins wrote:This is not a php issue - you have to do this in the client.

I would use a javascript function to trigger the page load in a new window (the forced download) then self.close() the current window.

Cheers,
Kieran
I have tried the javascript close methods but these have failed.
intergroove
Forum Commoner
Posts: 32
Joined: Wed Oct 11, 2006 4:42 pm

Post by intergroove »

volka wrote:
intergroove wrote:If I link directly to it my page refreshes to the blank download page.
Take a look at http://httpd.apache.org/download.cgi
There's a section "Apache HTTP Server x.y.z is the best available version" and a link e.g. to the unix sources. The link points to a mirror, not an apache.org server. Do you get a new,blank windows when clicking the link?

Much as I appreciate your help - at this juncture I am already past the deadline on my development, so brevity is the key.

If you have a prefered method then please explain it rather than send me on a teachers' style 'journey of discovery'.

I have spent far too long looking for a solution - I've found one that works - it would be nice to know the 'BEST' method but I really haven't got time for a goose chase.

Don't take this the wrong way - as you realise it gets pretty stressful when you go past your scheduled deadlines (+3 wks now).
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

intergroove wrote:at this juncture I am already past the deadline on my development, so brevity is the key.
Then spare me the rest of your previous post. Click the link and answer "yes, it opens a new window" or "no, it does not".
intergroove
Forum Commoner
Posts: 32
Joined: Wed Oct 11, 2006 4:42 pm

Post by intergroove »

Having said that Volka you are right. It launched a new window on the first few attempts (early on) so as I pointed out before - I made some assumptions.

If you had been explicit in your reply I might have cottoned on sooner...

But cheers anyway.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Ok, enough bickering.

@intergroove - In your popup window, since you are forcing the download, why not close the window after the download is forced to begin. Something like as the window is built (the HTML is pushed to the client) just before the closing BODY tag, put a call to a javaScript Function that you make called closeWindow(). Then in this function put the self.close() or window.close() call.

PS | This is a client side problem and is being moved to client side.
Post Reply