homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

Problem with JavaScript popup windows

 1:25 am on Jul 18, 2002 (gmt 0)


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:

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")
return false;

function KillWindow() {
<BODY ONUNLOAD="KillWindow()">

<FORM ACTION="http://www.asite.com/cgi-local/script.cgi" METHOD="post">
<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>


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.




 1:29 am on Jul 18, 2002 (gmt 0)

Welcome to WmW jerryxh,

I think you have to do upload.close();

win is the variable and not the window name.


 1:46 am on Jul 18, 2002 (gmt 0)

Thank you for your response jatar_k.

With your change, however, I now get the following error message:

Line: 14
Char: 3
Error: 'upload' is undefined
Code: 0

Any more ideas?



 1:52 am on Jul 18, 2002 (gmt 0)

what if you have an onBlur self.close(); in the popup and a onLoad=self.focus(); on the page it goes to when the script finishes?


 6:31 am on Jul 18, 2002 (gmt 0)

Do this instead:

Let the page with the form open the window the way it does now. But, let the page returned after processing the script close the window.

Your problem is that the object is not yet created when the script attempts to close it. :)


 9:25 am on Jul 18, 2002 (gmt 0)

I think you have to do upload.close();

win is the variable and not the window name.

This advice is wrong (as you have discovered).

win is a reference to the window object that was created, and contains all its properties and methods (such as document and close()). "upload" is the value of win.name -- it's a String object. It is also the value that would be used by any HTML target attribtes:

<a href="xyz.html" target="upload">


 3:35 pm on Jul 18, 2002 (gmt 0)


so you can clear me up then, is the win reference still available from any other page?

Is upload still not the name of the created window object and still available? or does assigning the window.open to a var mean that you can no longer use the name you opened it with for targeting?


 6:57 am on Jul 19, 2002 (gmt 0)

This example should clearify your questions:

<script language="JavaScript" type="text/javascript">
function openWin() {
win = window.open("about:blank","upload","width=200,height=200");
<a href="javascript:openWin()">Open 'upload'</a> ¦
<a href="javascript:upload.focus();">Focus 'upload'</a> ¦
<a href="javascript:upload.document.write('written');">Write 'upload'</a> ¦
<a href="javascript:upload.close();">Close 'upload'</a> ¦
<a href="javascript:win.focus();">Focus 'win'</a> ¦
<a href="javascript:win.document.write('written');">Write 'win'</a> ¦
<a href="javascript:win.close();">Close 'win'</a> ¦
<a href="javascript:document.write('targetted')" target="upload">Target 'upload'</a>


 1:51 pm on Jul 19, 2002 (gmt 0)

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");
else {
if (!win.closed) {

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 minor problem?



 3:53 pm on Jul 19, 2002 (gmt 0)

thx DrDoc,

I never knew that very interesting. It would seem that when assigning a window open to a var that the only way to interact with it is through that reference and not through it's name. hmm


 11:49 am on Jul 21, 2002 (gmt 0)

You have to be quite clear about the difference between a name and a reference to an object.

When you use JavaScript to create a window using window.open(), the function returns a window object. If you want to manipulate the window using JavaScript, you will need to store the object in a variable. The name of the window is one of the properties of the Window object.

When you type:

var myWindow=window.open('about:blank','popup','width=300,height=400');

that returns a Window object which is referenced by the variable myWindow. That object contains all the properties and methods you need to manipulate that window. For example:

myWindow.document.write('<h1>Hello world</h1>');
if(!myWindow.closed) valueForHTMLtargetAttribute=myWindow.name;

One of the many properties available in the Window object referenced by the variable myWindow is myWindow.name.

This is a String, not a Window. So you can use String methods and access String properties, but you can't use Window methods or properties on it. This is OK:

var lengthOfName=myWindow.name.length;

This is not:


If you try this:


it will open a window, and you will be able to target <a> tags using <a target="popup" ...>, but you won't be able to manipulate it with JavaScript because you cannot reference the Window object -- you have no access through JavaScript of the name of the new window, which is a string stored in the name property of the new Window object. But you have not stored a reference to the object anywhere: it exists, but you can't use it.

popup will not have been initialized as a variable; if you try

popup.document.write('<h1>Hello World</h1>');

you will get an error message to the effect that "popup.document has no properties". What this means is that the top-level variable popup does not contain an object called document -- not surprising, because it has only just been initialized and nothing has been assigned to it.

When working with client-side JavaScript, you need to fully understand what objects are and how windows and frames are referenced from each other. It can get very confusing -- as here -- which is why it is important also to learn the terminology.


 1:58 pm on Jul 21, 2002 (gmt 0)

Thanks rewboss. That's about the clearest explanation I've read for a tricky situation --and one I know I've stumbled over before.

I hope it stays clear in my mind for the next time I'm up against it. But I'm going to flag this thread, just in case my brain goes on vacation!


 8:43 pm on Jul 23, 2002 (gmt 0)

thank you rewboss for taking the time to explain that so well, greatly appreciated.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved