Click to See Complete Forum and Search --> : Check if a window is opened or closed.
coldsand
05-29-2003, 05:19 AM
I have a script to open up a window when the user submit a form.
window.open("","RESULT");
The submission will run a query using ASP and have the result return in Excel sheet form to the opened window.
Every time before the user hit the submit button, the user has to closed the "result" window so that the transaction will be successful else the result return will be null.
Is there a way to check if the RESULT window is opened?
Pls help.
Gollum
05-29-2003, 05:25 AM
or do you mean closed?
the Window object supports a read only boolean property called "closed"
so if you open a new window with..
myWin = window.open("", "RESULT");
and the user closes that window, myWin.closed will be true.
coldsand
05-29-2003, 06:30 AM
Gollum, thank you for the solution. Ya I have tried using the closed method. But I guess I used it wrongly.
window.open("","RESULT");
I used:
if (RESULT.closed) {
...
} else {
RESULT.closed;
}
I have learnt my mistake now. Once again, thank you Gollum for yuor help. I do appreciate it.
coldsand
05-29-2003, 07:22 PM
There is another problem.
function dosubmit(){
if !(mywin.closed) {
mywin.close();
}
mywin=window.open("",RESULT");
form.target = "RESULT";
form.submit();
}
My intention is to close the existing RESULT window before opening a new RESULT to display the data. However I get this error msg instead.
"mywin" is undefined
How do I get over with this?
this example might solved your problem:
var mywin;
function dosubmit()
{
if(mywin != null)
{
if (!(mywin.closed))
{
mywin.close();
}
}
mywin=window.open("http://www.google.com","Googles");
}
coldsand
05-30-2003, 08:40 AM
great! I shall try it out. Hey thanks a lot lot.
coldsand
06-09-2003, 10:30 PM
Originally posted by JHL
this example might solved your problem:
var mywin;
function dosubmit()
{
if(mywin != null)
{
if (!(mywin.closed))
{
mywin.close();
}
}
mywin=window.open("http://www.google.com","Googles");
}
Alas, it still doesnt work. I still get the message mywin is undefined. Any alternative?
change:
var mywin;
to:
var mywin = null;
coldsand
06-11-2003, 02:21 AM
Great, the statement Var myiwn=null; did solved the problem. But I have encountered another problem. Not sure if this is Javascript or ASP related problem.
Anyway, here it is.
mywin will opened another window whereby a form's action is directed to an asp. e.g. excelresult.asp. In the excelresult.asp the result is displayed as Excel Sheet. ie Response.ContentType = "application/vnd.ms-excel"
The problem arise when the user submit the form again while the mywin is still opened. mywin.closed returned true instead of false and thus mywin.close() will not trigger any action.
Instead I got a new error message saying a document excelresult.asp has already been opened.
What should I do to resolve this?
Gollum
06-11-2003, 04:23 AM
Interesting,
just had a go reproducing this and I noticed a number of things...
not only is mywin.closed true when the window is still open, but mywin.location.href is "about:blank" (at least on my machine), and a whole host of other properties were a bit unusual - as if the window and its document had nothing to do with the spreadsheet being displayed by the browser.
However, the game isn't over...
I also noticed that when the browser window is closed, those properties throw an exception, so you could write a function like this...
function IsMyWinOpen()
{
try
{
var loc = mywin.location.href;
}
catch(e)
{
return false;
}
return true;
}
and then...
if ( IsMyWinOpen() ) mywin.close();
coldsand
06-11-2003, 04:47 AM
The exception thing is very new to me. I didnt know that JSscript can actually have "try" and "catch". I thought only programming language like JAVA or C++ can do that.
anyway, thanks for this new trick. Cool!
There is another thg about this speadsheet in the new window. mywin.closed will not have any effect for the first time when the window is opened. i.e. in the first run when no window is opened, once the form is submitted, result is displayed in spreadsheet in a new window (mywin).
when the user submit the form again, mywin.close() will not have any impact. mywin still remains open.
an error msg that says document is still opened and the page will be displayed with an empty recordset.
But if the user were to submit the form again after that, mywin.close() will then take effect. The window will be closed and normal operation proceed.
This is really unexplainable... Do you have any idea why is this so?