Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

closing a popup window in code

Status
Not open for further replies.

mwa

Programmer
Jul 12, 2002
507
US
Hi everybody...

Here's a little background on what I'm doing so you can understand where I'm trying to go with this. I have a page that is doing a couple of things:
1. Create a text file that will be read by a crystal report.
2. Once the file has been created, I call a sub that does a window.open "crystalpage.asp","_self" to open the page that holds the crystal report
3. Also in that sub, I use window.open "StartDownload.asp", "_blank" to open a new window that will force the user to download the text file that was created(rather than displaying it to them in the browser and making them click file-->save as)

All 3 steps are working great but my question is on the StartDownload.asp page (step 3). How do I close this window after the file has been downloaded?

Below is my code for StartDownload.asp:
<%
Response.Buffer = True
Dim strFilePath, strFileSize, strFileName

Const adTypeBinary = 1

strFilePath = Request.QueryString(&quot;File&quot;)
strFileSize = Request.QueryString(&quot;Size&quot;)
strFileName = Request.QueryString(&quot;Name&quot;)

Response.Clear

Set objStream = Server.CreateObject(&quot;ADODB.Stream&quot;)
objStream.Open
objStream.Type = adTypeBinary
objStream.LoadFromFile strFilePath

strFileType = lcase(Right(strFileName, 4))

Case &quot;.txt&quot;
ContentType = &quot;text/plain&quot;
Case Else
'Handle All Other Files
ContentType = &quot;application/octet-stream&quot;
End Select


Response.AddHeader &quot;Content-Disposition&quot;, &quot;attachment; filename=&quot; & strFileName
Response.AddHeader &quot;Content-Length&quot;, strFileSize

Response.Charset = &quot;UTF-8&quot;
Response.ContentType = ContentType

Response.BinaryWrite objStream.Read
Response.Flush

objStream.Close
Set objStream = Nothing%>

I've also written the code that I wanted to display in the pop-window while the file was downloading:

<HTML>
<HEAD>
</HEAD>
<BODY onload=closewindow()>
<P align=center><font color=red size=3>This window should close automatically<BR>
after the file has been downloaded.<BR>
If it does not, please click the button below.<br></font>
<input type=button onclick=closewindow() value=Close name=button1 id=button1>
<SCRIPT language=vbscript>
sub closewindow()
window.close
end sub
</SCRIPT>


</BODY>
</HTML>

I thought I could put this after the line that says

Set objStream = Nothing%>

and then do another response.flush, but it justs adds all of the html to the text file and does not display it in the browser.

Any help with this would be great...

Thanks in advance,
mwa
 
[tt]I could be wrong, but I don't think one can close a pop-up with code w/o the user pressing &quot;yes&quot; or &quot;no&quot;

<body onload=&quot;setTimeout('window.close()',5000)&quot;>


this code above asks the user to close the window after 5 seconds.

I normally when trying to do what you're trying to do display a same sized window as the parent and add this code, making it look like the window automatically closes but it abviously re-directs...


<HEAD>
<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
redirTime = &quot;5000&quot;;
redirURL = &quot;yourpage.asp&quot;;
function redirTimer() { self.setTimeout(&quot;self.location.href = redirURL;&quot;,redirTime); }
</script>
'***************************************


<BODY onLoad=&quot;redirTimer()&quot;>
[tt]
&quot;The only ideas that will work for you are the ones you put to work.&quot;
[/tt]

 
I know that you can use the code in my sub above to close a window. I've used it before in a cleanup routine in another application.

I might be able to use that timeout that your suggesting, but I'm not sure where to put the html in my StartDownload.asp page.

No matter where I put it, it seems to get stepped on!!!

mwa
 
Hi,

Can you check your code again? You must have something wrong with code. For sure, popup window can be auto closed by itself.

I have one sample code as below:

<%
Function DestroyObjects(ByRef ObjectToDestroy)
If isobject(ObjectToDestroy) then
set ObjectToDestroy = nothing
DestroyObjects = true
Else
DestroyObjects = false
End if
End Function

'This function takes an object (by reference rather than by value) and destroys it.
'If the object was destroyed we return true otherwise we return false.

If DestroyObjects(session(&quot;oPageEngine&quot;)) then
' Response.Write(&quot;Page Engine Object Destroyed&quot; & &quot;<BR>&quot;)
Else
' Response.Write(&quot;Page Engine Object not in Scope&quot; & &quot;<BR>&quot;)
End If

If DestroyObjects(session(&quot;oRpt&quot;)) then
' Response.Write(&quot;Report Object Destroyed&quot; & &quot;<BR>&quot;)
Else
' Response.Write(&quot;Report Object not in Scope&quot; & &quot;<BR>&quot;)
End If

If DestroyObjects(session(&quot;oApp&quot;)) then
' Response.Write(&quot;Application Object Destroyed&quot; & &quot;<BR>&quot;)
Else
' Response.Write(&quot;Application Object not in Scope&quot; & &quot;<BR>&quot;)
End If
%>
<html>
<SCRIPT LANGUAGE=&quot;Javascript&quot;>

function CallClose()
{
window.close();
}
</SCRIPT>
<body Onload=&quot;CallClose();&quot;>
<%
Session.Abandon
Response.End
%>
</body>
</html>


It's provide by the third party company with web report function. This page has been used by crystal report developers all of the world.

Hope it helps !

Ted
 
Thanks... But I understand all of that...

My problem is in the StartDownload page. If I put any HTML on that page, it either doesn't get sent to the client, or it gets written into the actual text document that I'm trying to download. The HTML justs get appended to either the beginning or the end of the text.

I can't figure out where to put the html to make the window.close work.

Any other suggestions???
 
So with the above code creating the file download you actually have a window being displayed?
Are you opening the address directly into a new child window or are you opening the download page with a standard link/form in the same window?

Ie.
Window has the download link, onClicking it gets you window a new window where the download starts
vs
Window has the download link, onClicking it directsWindow to the download script and it starts to execute it


In my experience, as long as you opent the window ina completely new window all by itself, it will detect the fielstream and not create a new (blank) pop-up window, but instead will download the file (display file dialog), so you wouldn't need to worry about closing the window...
Please clarify for me, I may be a bit dull today :b

-Tarwn &quot;If you eat a live toad first thing in the morning, nothing worse will happen all day long.&quot; - California saying
&quot;To you or the toad&quot; - Niven's restatement of California saying
&quot;-well most of the time anyway...&quot; - programmers caveat to Niven's restatement of California saying
(The Wiz Biz - Ri
 
[tt]

For now why don't you create this pop-up window with the following code...

<html>
<head>
<title>Download Page</title>

</head>
<body bgcolor=&quot;#90FFFF&quot; onLoad=&quot;document.location = 'Downloads_Folder/File.XXX'&quot;>

<p>If download does not start automatically then click <a href=&quot;Downloads/File.XXX&quot;>here</a>.</p>

<p><b><a href=&quot;#&quot; onClick=&quot;window.close()&quot;>Close Window</a></b></p>
</body>
</html>


[tt]
&quot;The only ideas that will work for you are the ones you put to work.&quot;
[/tt]

 
Hey guys... Thanks for the response...

Tony - The code you gave me works but it just loads the text file into the users browser. Then the user has to click file->save as to get it onto their hard drive. I need to force the user to download the file.

Tarwn - I have three pages:
1. proc_proj_summary.asp - Builds text file then uses an onload of the body tag to call this sub
sub download(name,size,path)
newpage = &quot;StartDownload.asp?Name=&quot; & name & &quot;&Size=&quot; & size & &quot;&File=&quot; & path
window.open newpage, &quot;_blank&quot;
window.open &quot;view_proj_summary.asp&quot;, &quot;_self&quot;
end sub

2. view_proj_summary.asp - Displays Crystal report
3. StartDownload.asp - forces user to download text file built in proc_proj_summary.asp

All of this works perfectly. The only problem is once the user has downloaded the text file, The StartDownload.asp page just hangs there with nothing on it. The user has to click the close button to get rid of it. I was hoping their was a way to close this page automatically once the file has been downloaded.

thanks again,
mwa
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top