beginner: how to close a pop up window before opening an other pop up

Discussion in 'Javascript' started by Madame Blablavatsky, Sep 15, 2005.

  1. hello,

    i have a few links on a page. when one clicks a link an pop up window with
    an image opens.
    when people click an other link i want the already availible pop up window
    to close before
    the other pop up window opens.

    i just can't get it right, i always get an error message:
    line: 23
    char: 20
    error: object doesn't support this property or method

    the line with the error is:
    vensters[x].close();

    here is the source:
    ======================== source ====================
    <html>
    <head>

    <script style="text/javascript">
    // 'teller=1': no pop up window open;
    // 'teller=0': pop up window open;
    var teller;
    teller = 0;

    // array with pop up windows;
    var vensters = new Array(3);
    vensters[0] = "win1";
    vensters[1] = "win2";
    vensters[2] = "win3";

    function openWindowPlaatje(num) {
    var x;

    // if an pop up is already open, close it before opening a new pop
    up;
    if(teller == 1){
    for(x = 0; x < vensters.length; x++){
    if(vensters[x] != num ){
    vensters[x].close(); // this line gives error message;
    }
    }
    }



    // open the selected pop up;
    if(num=="win1"){
    win1=window.open("plaatje1.html","",'top=250,left=250,
    width=238,height=255');
    teller = 1;
    }

    if(num=="win2"){

    win2=window.open("plaatje2.html","","top=250,left=250,width=50,height=50");
    teller = 1;
    }

    if(num=="win3"){

    win3=window.open("plaatje3.html","","top=250,left=250,width=150,height=150")
    ;
    teller = 1;
    }

    }

    </script>
    </head>

    <body>

    <a href="JavaScript: openWindowPlaatje('win1')" >plaatje 1</a><br>
    <a href="JavaScript: openWindowPlaatje('win2')" >plaatje 2</a><br>
    <a href="JavaScript: openWindowPlaatje('win3')" >plaatje 3</a><br>

    </body>
    </html>
    =====================================================
    Madame Blablavatsky, Sep 15, 2005
    #1
    1. Advertising

  2. Madame Blablavatsky

    ASM Guest

    Re: beginner: how to close a pop up window before opening an otherpop up

    Madame Blablavatsky wrote:
    > hello,
    >
    > i have a few links on a page. when one clicks a link an pop up window with
    > an image opens.
    > when people click an other link i want the already availible pop up window
    > to close before
    > the other pop up window opens.


    correction :
    you didn't verify if popup to close was existing

    if(teller == 1){
    for(x = 0; x < vensters.length; x++){
    if(vensters[x] != num && vensters[x] && !vensters[x].closed){
    vensters[x].close(); // this line gives error message;
    }
    }
    }


    the best way would be all popups have same name (here : 'win')

    <html>
    <head>
    <script style="text/javascript">

    var win=false;

    function pop(page,attrib) {
    // if popup 'win' exists or ins't closed : close it
    if(win || !!win.closed) win.close();
    win = window.open(page,'',attrib);
    }

    function openWindowPlaatje(num) {
    if(num==1)
    pop("plaatje1.html","top=250,left=250,width=238,height=255");
    if(num==2)
    pop("plaatje2.html","top=250,left=250,width=50,height=50");
    if(num==3)
    pop("plaatje3.html","top=250,left=250,width=150,height=150");
    }

    </script>
    </head>

    <body>

    <a href="plaatje1.html" target="myWin"
    onclick="openWindowPlaatje(1); return false;" >plaatje 1</a><br>
    <a href="plaatje2.html" target="myWin"
    onclick="openWindowPlaatje(2); return false;" >plaatje 2</a><br>
    <a href="plaatje3.html" target="myWin"
    onclick="openWindowPlaatje(3); return false;" >plaatje 3</a><br>

    </body>
    </html>

    if javascript is disabled,
    visitor will get the right page in a new normal window

    --
    Stephane Moriaux et son [moins] vieux Mac
    ASM, Sep 15, 2005
    #2
    1. Advertising

  3. thanks for your help, it is now working as it should

    blablavatksy
    Madame Blablavatsky, Sep 15, 2005
    #3

  4. >
    > function pop(page,attrib) {
    > // if popup 'win' exists or ins't closed : close it
    > if(win || !!win.closed) win.close();
    > win = window.open(page,'',attrib);
    > }


    Hello Stephane Moriaux,

    I find it a very clever solution but i still have one question:
    what is the meaning of the double exclamation mark?
    if(win || !!win.closed) win.close();

    At first I thought it to be a mistake, but when I omitted it, it gave an
    error message.
    I can knowwhere find the meaning of it.

    thanks
    Madame Blablavatsky, Sep 15, 2005
    #4
  5. Madame Blablavatsky

    ASM Guest

    Re: beginner: how to close a pop up window before opening an otherpop up

    Madame Blablavatsky wrote:
    >>function pop(page,attrib) {
    >> // if popup 'win' exists or ins't closed : close it
    >> if(win || !!win.closed) win.close();
    >> win = window.open(page,'',attrib);
    >>}

    >
    >
    > Hello Stephane Moriaux,
    >
    > I find it a very clever solution but i still have one question:
    > what is the meaning of the double exclamation mark?
    > if(win || !!win.closed) win.close();
    >
    > At first I thought it to be a mistake, but when I omitted it, it gave an
    > error message.


    so ... now you know why they are

    > I can knowwhere find the meaning of it.


    ! want to say 'no'

    experiment :

    <html>
    <script>
    var asm = true;
    alert( !asm ) ; // --> false
    alert( !!asm ); // --> true
    </script>
    </html>

    --
    Stephane Moriaux et son [moins] vieux Mac
    ASM, Sep 15, 2005
    #5
  6. Madame Blablavatsky

    ASM Guest

    Re: beginner: how to close a pop up window before opening an otherpop up

    Zoe Brown wrote:
    > "Madame Blablavatsky" <> wrote in message
    > news:432977a8$0$18288$...
    >
    >>what is the meaning of the double exclamation mark?

    >
    >
    > or.


    not at all

    --> 'or' = ||
    --> 'and' = &

    and ! (exclamation mark)

    --> 'no' = !


    --
    Stephane Moriaux et son [moins] vieux Mac
    ASM, Sep 15, 2005
    #6

  7. > <html>
    > <script>
    > var asm = true;
    > alert( !asm ) ; // --> false
    > alert( !!asm ); // --> true
    > </script>
    > </html>


    thanks for your help and explanation, it is clear to me now.

    blablavatsky
    Madame Blablavatsky, Sep 15, 2005
    #7
  8. Madame Blablavatsky

    Donius Guest

    So, semantically, it's useless, right? I'm not missing anything here?
    It just means 'not not', which cancels itself out?

    -Brendan
    Donius, Sep 16, 2005
    #8
  9. JRS: In article <>,
    dated Fri, 16 Sep 2005 06:48:03, seen in news:comp.lang.javascript,
    Donius <> posted :
    >So, semantically, it's useless, right? I'm not missing anything here?
    >It just means 'not not', which cancels itself out?


    No. Yes. Sort of.

    It returns a Boolean, whatever it may have been given; and a Boolean is
    the right way to represent a two-way yes/no-type quantity. It does so
    whether it is given on its right a number, string, function name,
    undefined, NaN, +-infinity, and UTC&A&A.

    However, in the context
    if (win || !!win.closed) win.close();
    in which it was introduced in this thread, ISTM superfluous; win.closed
    should already be Boolean, and, if it were not, the context would coerce
    it - its author is already relying on the context to coerce a function
    reference or undefined to Boolean.

    --
    © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
    <URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
    <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
    Dr John Stockton, Sep 16, 2005
    #9
    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. J P Singh
    Replies:
    3
    Views:
    288
    Howard Rothenburg
    Feb 11, 2004
  2. Iñaki Baz Castillo
    Replies:
    7
    Views:
    821
    Iñaki Baz Castillo
    Jan 12, 2010
  3. Julia Briggs
    Replies:
    1
    Views:
    93
    G Roydor
    Jul 5, 2004
  4. Shang Wenbin

    close current window using window.close()

    Shang Wenbin, Sep 1, 2005, in forum: Javascript
    Replies:
    10
    Views:
    351
    Gérard Talbot
    Sep 2, 2005
  5. Howard Martin

    Opening a pop-up from a pop-up

    Howard Martin, Jul 11, 2007, in forum: Javascript
    Replies:
    2
    Views:
    76
    Howard Martin
    Jul 12, 2007
Loading...

Share This Page