Jump to content

close a popup window


nasi

Recommended Posts

HiI need to close a previously opened popup window ondblclick and open a new one. I have written the follwoing code but it doesn't work. can you please help me?function popup(width,height){ popWin=open('', 'new_window', attr); some codes }function closepopup() { if( popWin.open) { popWin.close (); } }onload = function () { document.getElementById("textarea").ondblclick = function () {closepopup(); popup(200,200)}}

Link to comment
Share on other sites

I'm not sure if the if statement will work, the open method is a function so I'm not sure if testing that will return true or false. The window object has a closed property that will tell you whether or not the window has been closed.if( !popWin.closed)

Link to comment
Share on other sites

I'm not sure if the if statement will work, the open method is a function so I'm not sure if testing that will return true or false. The window object has a closed property that will tell you whether or not the window has been closed.if( !popWin.closed)
I tested your suggestion, but It doesn't work either.
Link to comment
Share on other sites

Have you verified that the dblclick event is even firing?
yest it is, because when I delete "closepopup" from the "document.getElementById("textarea").ondblclick = function () {closepopup(); popup(200,200)}", it works properly.
Link to comment
Share on other sites

I think the problem is that I am closing a popWin before I open it! but I am a bit confused. whenever I want to open a new popup window ondblclick I need to check if it is previously opened or not. so before opening the second window I should close the first window. but for the first time that I dblclick and open a window, there is no previous window and it makes the problem. do you have any idea how I should fix it?Thanks

Link to comment
Share on other sites

I think the problem is that I am closing a popWin before I open it! but I am a bit confused. whenever I want to open a new popup window ondblclick I need to check if it is previously opened or not. so before opening the second window I should close the first window. but for the first time that I dblclick and open a window, there is no previous window and it makes the problem. do you have any idea how I should fix it?
You have to define popWin in the global space and modify your conditional in closepopup to also check for that default value.
var popWin = null;function popup(width,height) {   popWin=open('', 'new_window', attr);   some codes}function closepopup() {   if((popWin !== null) &&  (!popWin.closed)) {	  popWin.close ();   }}

EDIT:Are you sure you're not getting any errors? Because if this is indeed the problem you should get an error saying something about popWin being undefined.

Link to comment
Share on other sites

You have to define popWin in the global space and modify your conditional in closepopup to also check for that default value.
var popWin = null;function popup(width,height) {   popWin=open('', 'new_window', attr);   some codes}function closepopup() {   if((popWin !== null) &&  (!popWin.closed)) {	  popWin.close ();   }}

EDIT:Are you sure you're not getting any errors? Because if this is indeed the problem you should get an error saying something about popWin being undefined.

great. thanks for your help. now it works. now the problem that I really don't understand why happens is that when I dblclick a word a popupWin opens, when I dblclick for the second time the previous popWin closes but the new one doesn't open and I need to dblclick for the third time to open new popWin. :)
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...