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

  • Thread starter Madame Blablavatsky
  • Start date
M

Madame Blablavatsky

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>
=====================================================
 
A

ASM

Madame said:
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
 
M

Madame Blablavatsky

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
 
A

ASM

Madame said:
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>
 
M

Madame Blablavatsky

<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
 
D

Donius

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

-Brendan
 
D

Dr John Stockton

JRS: In article <[email protected]>,
dated Fri, 16 Sep 2005 06:48:03, seen in
Donius said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,222
Members
46,810
Latest member
Kassie0918

Latest Threads

Top