Can I clear the content of a window, or close it if it's not open.

Discussion in 'Javascript' started by Guy, Dec 13, 2003.

  1. Guy

    Guy Guest

    I have 4 images on a page and when someone clicks on one of them I open a
    secondary window and write a few lines to it describing the image.

    If they don't close that secondary window however, and click on another of
    these 4 images, my write commands print further down on that secondary
    window.

    I was reading through an old javascript book that talks about the
    win1.document.clear(); method, but information on the Internet tells me that
    this is now obsolete, or was just working on Netscape.

    So then I decided to close the secondary window before re-opening it. But if
    the user _did_ close the secondary window, I get an error saying that the
    window I'm trying to close is already closed (or doesn't exist to be exact)!

    Is there a way to clear the document in a window that I opened and wrote to?
    Or is there a way to detect if win1 exists or not? If it exists, I would
    close it. If it didn't exist, I would just re-open a new one.

    Thanks for all.
    Guy
     
    Guy, Dec 13, 2003
    #1
    1. Advertising

  2. Re: Can I clear the content of a window, or close it if it's notopen.

    Guy wrote:

    > I have 4 images on a page and when someone clicks on one of them I open a
    > secondary window and write a few lines to it describing the image.


    What about popup blockers? What about people without a
    UA who has client-side JavaScript support (enabled)?

    > If they don't close that secondary window however, and click on another of
    > these 4 images, my write commands print further down on that secondary
    > window.


    If you need only a popup, use target="foobar" for the a element.

    If you need a special popup, search Google Groups for window.open.
    This one is quite a FAQ. Once you have named a window it is reused
    if already open and the same name is used.

    > So then I decided to close the secondary window before re-opening it.


    Bad idea because it eats system resources in flawed implementations
    without (completely) freeing them afterwards.

    > But if the user _did_ close the secondary window, I get an error saying that the
    > window I'm trying to close is already closed (or doesn't exist to be exact)!


    Ask Google about the `closed' property.

    > Is there a way to clear the document in a window that I opened and wrote to?


    Depends on how its content is composed. Generally, a
    document.write(...) after load will overwrite the document
    but as you do not want to exclude users without JavaScript,
    you will not need that.

    > Or is there a way to detect if win1 exists or not?


    There is, search before you post.

    > If it exists, I would close it.


    Use the close() method then.

    > If it didn't exist, I would just re-open a new one.


    This is not always necessary nor recommended.


    PointedEars
     
    Thomas 'PointedEars' Lahn, Dec 14, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?a2x5bm4=?=

    open window & close prior window

    =?Utf-8?B?a2x5bm4=?=, Oct 7, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    526
    =?Utf-8?B?a2x5bm4=?=
    Oct 7, 2005
  2. Dave
    Replies:
    11
    Views:
    5,162
  3. Iñaki Baz Castillo
    Replies:
    7
    Views:
    937
    Iñaki Baz Castillo
    Jan 12, 2010
  4. Shang Wenbin

    close current window using window.close()

    Shang Wenbin, Sep 1, 2005, in forum: Javascript
    Replies:
    10
    Views:
    422
    Gérard Talbot
    Sep 2, 2005
  5. Iulian Ilea
    Replies:
    1
    Views:
    340
    pcx99
    Dec 21, 2006
Loading...

Share This Page