PDA

View Full Version : Problem with opening and closing a JavaScript popup windo


jerryxh
07-18-2002, 05:58 AM
Hello-

I am having trouble with the following piece of JavaScript code, which is supposed to open a new window when the submit button is clicked and then close the window after the cgi script has completed processing:

<HTML>
<HEAD>
<TITLE></TITLE>
<SCRIPT LANGUAGE="JavaScript">
<!--
var win;
function OpenWindow() {
win = window.open("http://www.asite.com","upload","toolbar=no,location=no,directories=no,menubar=no,scrollbars=no,width=290,height=170,left=665,top=440")
document.forms[0].submit();
return false;
}

function KillWindow() {
win.close();
}
//-->
</SCRIPT>
</HEAD>
<BODY ONUNLOAD="KillWindow()">

<FORM ACTION="http://www.asite.com/cgi-local/script.cgi" METHOD="post">
<TABLE BORDER="0" CELLPADDING="1" CELLSPACING="1">
<TR><TD>Filename</TD></TR>
<TR><TD><INPUT TYPE="file" SIZE="22" NAME="filename"></TD></TR>
<TR><TD ALIGN="center"><INPUT TYPE="button" VALUE="Submit" ONCLICK="return OpenWindow()"></TD></TR>
</TABLE>
</FORM>

</BODY>
</HTML>


When I run this code in IE, I get the following script error:

Line: 14
Char: 3
Error: 'win' is null or not an object
Code: 0

I would appreciate very much any help I can get with this problem.

Jerry

Cloudski
07-18-2002, 07:37 AM
Originally posted by jerryxh

function KillWindow() {
win.close();
}


When I run this code in IE, I get the following script error:

Line: 14
Char: 3
Error: 'win' is null or not an object
Code: 0


Well, I am not sure you can close the pop-up from here... or I just don't know how. I do know that you have to put something else where win.close() is, but not sure what. What you could do, is put the following code for after the CGI finished, in the pop-up itself:


window.close()


But then again, I am not fully sure as to what exactly is going on, but I hope I helped a little..

jerryxh
07-19-2002, 02:46 PM
Thanks for everyones help. I did come up with a solution and it involves replacing the KillWindow() function with:

function KillWindow() {
if (typeof win != "object") {
win = window.open("", "upload");
win.close();
}
else {
if (!win.closed) {
win.close();
}
}
}

The only minor problem with this solution is that in Internet Explorer (but not in Netscape) the line

win = window.open("", "upload");

causes a small window to open up for a fraction of a second each time a link or button on the page is clicked. Does anyone know if there is a way to to work around this?

Jerry