473,796 Members | 2,488 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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="JavaS cript">

function dlFile(file)
{
var win
=window.open(fi le,'dlnow','too lbar=0,location =no,directories =0,status=0,
scrollbars=no,r esizable=yes,wi dth=1,height=1, top=0,left=0');
win.focus();
// win.close(); // Causes the program to die before the DL starts
}
var dlnl = "dlFile('ht tp://myserver/myprog.php?Open =127967')";
document.write( '<a href="javascrip t:' + dlnl + '">Download</a>');
</script>

Jul 23 '05 #1
4 2145
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="JavaS cript">

function dlFile(file)
{
var win
=window.open(fi le,'dlnow','too lbar=0,location =no,directories =0,status=0,
scrollbars=no,r esizable=yes,wi dth=1,height=1, top=0,left=0');
win.focus();
// win.close(); // Causes the program to die before the DL starts
}
var dlnl = "dlFile('ht tp://myserver/myprog.php?Open =127967')";
document.write( '<a href="javascrip t:' + 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('ht tp://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.ph p?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_f ile_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_f ile_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($listF ile));
# 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*****@agrico reunited.com>
comp.lang.javas cript 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*****@agrico reunited.com> wrote in message
news:41******** *******@agricor eunited.com...
Chuck Tomasi wrote:
var dlnl = "dlFile('ht tp://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.ph p?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_f ile_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_f ile_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($listF ile));
# 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*****@agrico reunited.com>
comp.lang.javas cript 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
5648
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 window when I open a new window. > > function expand() { > window.moveTo(0,0); > window.resizeTo(screen.availWidth, screen.availHeight); > }
19
31075
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(). The problem is that after the reload, it brings you right to the top of the page. When I click 'refresh" on the original page, it brings me back to the original viewing position. Is there a way to duplicate this in from the popup window. Also,...
14
11099
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 window.open function? I would prefer not to create a separate HTML page. So far all I have is the basic var cwin = window.open('images/KJV-THANKS.gif', 'Thanks', 'width=243,height=420,'); cwin.focus();
3
2546
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 and recieving data and call the Close() method the socket lingers for a couple of minutes. This is demonstrated by using netstat -a. I've tried an assortment of SocketOptions (Linger, and DontLinger), but
1
11590
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 http://weblogs.asp.net/asmith/archive/2003/09/15/27684.aspx but it was far more complex then I needed. (I got lost trying to figure it all out). Therefore, here goes my simple "web dialog box with parent event handler fireing" solution. ...
26
5694
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 several parts of the DOM, but this does not include the window object. Thank you
4
6411
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 to look a and I am loosing tremendious amounts of time trying to organize it so that I could view it. Regards, Alexandre Brisebois
5
7535
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 click the attach it should upload file and close the window and send back control to Main page.. But it is not happening.. CODE: ========= <script LANGUAGE="JavaScript"> if ((window.opener != null) && (! window.opener.closed)) {
24
8231
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 seems to have a (tiny) amount overhead since window itself points to the global object, hence, a circular reference. (From everything I am reading, window is just a REFERENCE back to the global object, as opposed to a separate object.)
0
9683
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9529
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10231
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10176
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9054
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6792
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5576
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4119
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3733
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.