I have created a new window using some_var=window.open(...). This new window may be created by the visitor or not. It's an optional new window.
But I want to close this new window when the main window (the one that created it) is closed or unloaded. So, I used the event onunload to try to close it but I'm having problems.
There can have three possible situations:
1 - the main window is closed/unloaded and the new window was never created, so some_var is still undefined;
2 - the main window is closed/unloaded and the new window was already created but it has been closed prior to closing/unloading the main window. some_var is not anymore undefined and I can close the new window just by using some_var.close() on the onunload event of the main window;
3 - the main window is closed/unloaded and the new window is open; situation similar to "2", so I just use some_var.close() on the onunload event of the main window.
So, my problem is the situation "1". If I try to close some_var, I'll get an error, since some_var is undefined. But I don't know how to avoid trying to close some_var because I don't know how to detect if it is undefined or not. I have tried using if( some_var != null) but this doesn't work either.
What am I doing wrong? Is there another smarter way of doing this stuff I want to do?
TIA