Results 1 to 6 of 6
  1. #1
    Lounger
    Join Date
    Mar 2002
    Location
    Tampa, Florida, USA
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts

    JavaScript: Closing a window

    I have a link in a main (probably fullscreen) window which, upon request, opens up a small, popup window. In that window, I display some stuff, open another page in that small window, and gather some data in a form. Upon submission, I call a CGI script (actually a PHP script, but it doesn't really matter), but upon submission I open the new page in the original window by using the target parameter. That's no problem (though it took me the better part of a day to get it working).

    But... that leaves my little popup window still open. After the user clicks submit and I pass control to the script, I want that window to close.

    I can get it to close in IE. Invoking a JavaScript routine via an onSubmit in the form statement, I can code window.close() in the subroutine. IE (version 4 or 5) will close the window and submit the form to the CCI.

    But in Netscape (version 6.. and I presume earlier ones), NN just closes the window without submitting the form. So I guess instead of closing the window before the submit, I would have to use JavaScript in the page generated by the CGI to close the window. But I cannot figure out what the syntax is to do so.... or even if it is possible.

    It must be possible... This is not so difficult, is it?

    Apparently so... for me.

    Anydoby got any ideas?

    Thanks,

    - hmc

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: JavaScript: Closing a window

    Typically you create a reference to the new window when you first open it:

    newwin = window.open(parameters)

    See if newwin is still a good variable after you load the new document. I think it probably is, since the old window has merely changed its contents, but I haven't tested it. If so, you should be able to close it using your usual technique.

  3. #3
    Lounger
    Join Date
    Mar 2002
    Location
    Tampa, Florida, USA
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: JavaScript: Closing a window

    I'm lost as to what you are referring to as "new" and "old," and what my "usual technique" is. It is not a matter of whether the variable is still good, it is a matter of sequence of events.

    If I try to close the small, popup window before I submit the form, Netscape does not submit the form. And I cannot figure out what the right syntax would be to close it in the original window after the page has been built. I have always closed a window with "window.close()" - but if course, that closes the current window. I have named the small popup window "pop2," but I cannot figure out the right syntax to close the popup window from the new content generated by the CGI program. None of these work:
    window.pop2.close()
    pop2.window.close()
    window.parent.close()

    Thanks,

    - hmc

  4. #4
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: JavaScript: Closing a window

    Sorry, I can't find my JavaScript book, so I'm pretty useless right now. I did a test that confirms that unfortunately, the new document has no idea about the variable set in the first document, so what I wanted to do won't work.

  5. #5
    Lounger
    Join Date
    Mar 2002
    Location
    Tampa, Florida, USA
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: JavaScript: Closing a window

    Thanks for giving it a try. I have been beating my brains out on this one all week. Here is what I have found:

    When you open windows using JavaScript, i.e.:
    objname = open(url,windname,properties)
    you can use windname to refer to the window in other windows, using JavaScript.
    But when you open a window (maybe that is a carelss way of stating it... but I have been up all night) via a form submit to a CGI program, he knows nothing about window names or any other windows. All he knows is himself. Even if you pass the windname as a form variable to the page in the CGI program, you cannot refer to that window. At least, that is what my research and testing has led me to conclude. So that means I cannot close the small window in the page that was generated by my CGI program. I have to do it from within the small window.

    One way to deal with that limitation is to build the final page in the small window rather than calling a CGI program:

    origwind= open("", "originalwind")
    origwind.document.write("<html><body>build the content here - this is a pain, though

    </body></html>")
    origwind.document.close(); // close the document - (not the window!)
    and then close the small window:
    window.close()

    I think that would work. But it is very awkward, and as I recall, the IE browser used in AOL 4.0 had troubles with windows created this way. (Though I am not exactly creating a window here... just writing new content to an existing window.) Anyway, I would rather avoid it.

    Back to the original plan:

    I have found that if I use an onSubmit event handler in the form statement and call a subroutine, I can submit the form from the subroutine, and close the window in the statement following it:

    document.form2.submit()
    window.close()

    I'm still testing, but I think this works. I am having trouble with IE 4.0, but I think it'll work.

    I have bought enough of his books to pay for a new wing on Danny Goodman's palacial mansion, but as far as I can tell, none of his books gets into this. I could not find it explained at webmonkey, irt, javascript, webdeveloper, MSDN, or even at Drudgereport.com. But... maybe I missed something.

    It never ends, does it?

  6. #6
    Silver Lounger
    Join Date
    Dec 2000
    Location
    Los Angeles, California, USA
    Posts
    1,734
    Thanks
    0
    Thanked 3 Times in 1 Post

    Re: JavaScript: Closing a window

    My Javascript is very much a cut-and-paste experience but have a look at how I do it in the On-the-fly-Login we have here:
    Logout; go to any board or message and click the NewPost (or Newreply) button.
    This pops a window where you can login and when you submit it, it opens the message form in the main widow and closes itself.
    <IMG SRC=http://www.wopr.com/w3tuserpics/Eileen_sig.gif>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •