473,396 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Lingering Window

I'm clearly missing something and I've been looking for a solution for hours
without a clue.

Objective: Create a typical "download" link using JS window.open. I get the
window to open, the "Save As" box appears, and the file ends up where I want
it, with the right name, the problem is that the child window doesn't go
away (save IE). Mozilla Firefox (PC and Mac), Safari, and all others I've
tested leave the child window. If I do a win.close() the child window
appears and disappears so fast that the SaveAs never shows up. I've tried
using a setTimeout() call to delay this a bit until everything is ready, but
that doesn't do it. What am I missing? Do I need a redirect? If so, where?
I'd like to leave the original page intact/untouched.

Here's my code snippet. Assume "myprog.php" has all the proper sense to send
the right HTTP headers and such.

<script language="JavaScript">

function dlFile(file)
{
var win
=window.open(file,'dlnow','toolbar=0,location=no,d irectories=0,status=0,
scrollbars=no,resizable=yes,width=1,height=1,top=0 ,left=0');
win.focus();
// win.close(); // Causes the program to die before the DL starts
}
var dlnl = "dlFile('http://myserver/myprog.php?Open=127967')";
document.write('<a href="javascript:' + dlnl + '">Download</a>');
</script>

Jul 23 '05 #1
4 2125
I had had the same problem as you and I haven't found any solution so far.
If you know that the type of the file that's going to be downloaded is not a
type that can be displayed inside any browser (unlike htm, txt or pdf), a
simple link inside your original page will work.
The problem is with .doc files, IE displays them inside the browser, Mozilla
doesn't...
So the best way is still to open a new window like you did and let the user
close it if the downloaded file doesn't get displayed inside it.

Henri
"Chuck Tomasi" <ct*****@new.rr.com> a écrit dans le message de
news:ZV***********@twister.rdc-kc.rr.com...
I'm clearly missing something and I've been looking for a solution for hours without a clue.

Objective: Create a typical "download" link using JS window.open. I get the window to open, the "Save As" box appears, and the file ends up where I want it, with the right name, the problem is that the child window doesn't go
away (save IE). Mozilla Firefox (PC and Mac), Safari, and all others I've
tested leave the child window. If I do a win.close() the child window
appears and disappears so fast that the SaveAs never shows up. I've tried
using a setTimeout() call to delay this a bit until everything is ready, but that doesn't do it. What am I missing? Do I need a redirect? If so, where? I'd like to leave the original page intact/untouched.

Here's my code snippet. Assume "myprog.php" has all the proper sense to send the right HTTP headers and such.

<script language="JavaScript">

function dlFile(file)
{
var win
=window.open(file,'dlnow','toolbar=0,location=no,d irectories=0,status=0,
scrollbars=no,resizable=yes,width=1,height=1,top=0 ,left=0');
win.focus();
// win.close(); // Causes the program to die before the DL starts
}
var dlnl = "dlFile('http://myserver/myprog.php?Open=127967')";
document.write('<a href="javascript:' + dlnl + '">Download</a>');
</script>


Jul 23 '05 #2

The file is XML and unfortunately a lot of browsers like to DISPLAY XML
with a
simple link so the window.open, run the PHP program that generate the
XML
(with proper HTTP headers) is currently my only option to leave the
original
window intact. My JS code is based largely upon that used by
download.com,
however their download() function uses a couple additional args for
redirection. They send you to one of those "Your download should begin
automatically pages..." I'd rather not do that since I'm only trying to
download an XML synpopsis of the page displayed. I use this in an
issue
tracking system and the XML eventually goes to a 'task manager' type
program.
<p>

One would think that downloading a file shouldn't be this hard with the
years
HTML/JS have had to mature.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 23 '05 #3
Chuck Tomasi wrote:
var dlnl = "dlFile('http://myserver/myprog.php?Open=127967')";


You shouldn't be trying to do this through client-side JavaScript at all. In
fact, you don't need to open a new window to protect the current page. Have
myprog.php output the appropriate Content-Disposition header to avoid having the
browser process the file (even if it knows how). You mention the files are XML?
If you always want to force the download, the simplest thing to do is tell the
browser it's a binary file, then no browser should ever try to interpret the
content:

<a href="myprog.php?Open=127967">Download</a>

Then myprog.php does something like:

<?php

# obtain the information about the file referred to by 'Open'
# including it's name and it's size
# $the_file_name = {the name};
# $the_file_size = filesize($the_file_name);

# to absolutely force almost all browsers to want to save the file
header("Content-Type: application/octet-stream");
# or if you want the browser to properly identify the XML file type
# (you run the risk that some little-used browser will want to parse the file
itself)
# header("Content-Type: text/xml");
header("Content-Disposition: attachment; filename=$the_file_name");
header("Content-Length: $the_file_size");

# open, read and print the file to the client
if ($fp = fopen($the_file_name, "r")) {
while (!feof($fp)) {
$buffer = fgets($fp, 1024);
print "$buffer";
}
fclose($fp);
}
# or if the files aren't too large
# if ($fp = fopen($listFile, "r")) {
# $buffer = fread($fp, filesize($listFile));
# print "$buffer";
# fclose($fp);
# }

?>

Now, clicking the link will present the user with a Save As dialog in almost all
browsers and the current page they are viewing will not be replaced or reloaded.
The above will present a Save As dialog in IE 4+, all recent Gecko-based
browsers, Opera 6+, etc. When I refer to browsers it may not work in, I'm
talking about "Sam's Favorite Home Written Browser" run by 3 people on the
planet (although there may be slightly more popular browsers that do not
properly support Content-Disposition as well).

No reliance on client-side technology at all.

--
Grant Wagner <gw*****@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq

Jul 23 '05 #4
That's exactly what I needed. I just tried it (with no modifications to the
PHP code) and it worked. Thanks. I thought I had tried this approach before
and failed which is why I went to client side JS.

--Chuck

"Grant Wagner" <gw*****@agricoreunited.com> wrote in message
news:41***************@agricoreunited.com...
Chuck Tomasi wrote:
var dlnl = "dlFile('http://myserver/myprog.php?Open=127967')";
You shouldn't be trying to do this through client-side JavaScript at all.

In fact, you don't need to open a new window to protect the current page. Have myprog.php output the appropriate Content-Disposition header to avoid having the browser process the file (even if it knows how). You mention the files are XML? If you always want to force the download, the simplest thing to do is tell the browser it's a binary file, then no browser should ever try to interpret the content:

<a href="myprog.php?Open=127967">Download</a>

Then myprog.php does something like:

<?php

# obtain the information about the file referred to by 'Open'
# including it's name and it's size
# $the_file_name = {the name};
# $the_file_size = filesize($the_file_name);

# to absolutely force almost all browsers to want to save the file
header("Content-Type: application/octet-stream");
# or if you want the browser to properly identify the XML file type
# (you run the risk that some little-used browser will want to parse the file itself)
# header("Content-Type: text/xml");
header("Content-Disposition: attachment; filename=$the_file_name");
header("Content-Length: $the_file_size");

# open, read and print the file to the client
if ($fp = fopen($the_file_name, "r")) {
while (!feof($fp)) {
$buffer = fgets($fp, 1024);
print "$buffer";
}
fclose($fp);
}
# or if the files aren't too large
# if ($fp = fopen($listFile, "r")) {
# $buffer = fread($fp, filesize($listFile));
# print "$buffer";
# fclose($fp);
# }

?>

Now, clicking the link will present the user with a Save As dialog in almost all browsers and the current page they are viewing will not be replaced or reloaded. The above will present a Save As dialog in IE 4+, all recent Gecko-based
browsers, Opera 6+, etc. When I refer to browsers it may not work in, I'm
talking about "Sam's Favorite Home Written Browser" run by 3 people on the
planet (although there may be slightly more popular browsers that do not
properly support Content-Disposition as well).

No reliance on client-side technology at all.

--
Grant Wagner <gw*****@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq

Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: David Hayes | last post by:
juglesh <juglesh@nospamRadioKDUG.com> wrote in "Re: how to maximize the browser window that fits the monitor size?" (Saturday, January 01, 2005 3:12 AM): > > >I want to maximize the browser...
19
by: Darren | last post by:
I have a page that opens a popup window and within the window, some databse info is submitted and the window closes. It then refreshes the original window using window.opener.location.reload(). ...
14
by: D. Alvarado | last post by:
Hello, I am trying to open a window containing an image and I would like the image to be flush against the window -- i.e. have no padding or border. Can I make this happen with a single call to a...
3
by: Jimbo | last post by:
Hi Guys, I'm sorry if you have heard this one before but searching the net hasn't found a solution. I am using Socket in a client to connect to a server. However, after I've finished sending...
1
by: Earl Teigrob | last post by:
I did a ton of searching to try and find a simple solution to this issue and finally wrote my own, which I am sharing with everyone. In my searching, I did find a very complete and robust solution at...
26
by: yb | last post by:
Hi, Is there a standard for the global 'window' object in browsers? For example, it supports methods such as setInterval and clearInterval, and several others. I know that w3c standardized...
4
by: alexandre.brisebois | last post by:
Hi, I am using access 2003, I would like to know if there is an option to reorganize the tables in a maner that is readable, as we can do in sql sever 2000 or 2005. I have been given a database...
5
by: asadhkhan | last post by:
I have the following code which works correctly in IE 6, but in IE 7, Fire Fox 2.0 and Netscape 8 it does not work. I have a main page where a button calls this pop-up and uploads a file once you...
24
by: Jeremy J Starcher | last post by:
While reading c.l.j, I've noticed that some people prefer and indeed even recommend the use of "window.alert()" over "alert()". I can't find any technical reason to make this distinction, and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.