Link to home
Start Free TrialLog in
Avatar of coesy
coesy

asked on

Closing Multiple Browser Windows

Hopefully someone may have an idea here.

We have a C# ASP.NET Application, this app can spawn many child windows from one main window, anything up to 5 levels. What we would really like is if someone clicks on logout on the main window, it knows what children are open and closes these down automatically and logs out on the main window, likewise if the session times out, the next time someone clicks on the app (on any level) it will close down all child windows and logout on the main window. I hope this makes some sort of sense and any ideas would be appreciated.

Thanks
Avatar of rangasuman
rangasuman

You can achieve this using Javascript.
You would have to keep a reference in the parent window of any child windows opened to achieve this.

<INPUT TYPE=button onClick="child1=window.open ('http://www.google.com','child1')" VALUE="Open">
<INPUT TYPE=button onClick="child2=window.open ('http://www.yahoo.com','child2')" VALUE="Open">
<INPUT TYPE=button onClick="child1.close();child2.close()" VALUE="Close All">

You can add as many as windows you would like with a different reference.
Avatar of coesy

ASKER

This is ok but with our app any combination of windows could be open and we don't neccessarily know which ones are being used at the time of logout or session end.
I did not understand what do you mean by that. If you dont know how many windows you would like to open, then you need to initialize an array for the reference and then you can loop to close the windows. If you want to selectively close only some windows, you can still do so.
Avatar of coesy

ASKER

Users open windows by clicking links i don't force any windows to open so i don't know which ones are open, its basically a web app that works like a windows app, so multiple forms could be open at once, its a good idea about the array though, but i am not sure how best to store this between forms, would it be better client side or server side? would you use cookies or something? also how would you know whether a form has been closed by the user or not in order to remove it from the array, i know about the onunload function but this works on both refresh and close? sorry just trying to get it clear in my head.
Since you would like to close all the windows, you can simply use the code snippet written above. If you call a window to close and if does not exist, it should not matter. If you initialise an array, it will be on the client side with in the Javascript code and will be valid till some one closes the main window containing the code. There is no need to store this value in a cookie or any where else. There is no way we can know if a user closes a window and we would know about it. So, I suggest you to not get into array trouble unless you are good at Javascript.
Avatar of coesy

ASKER

ok, thats fine. so if i keep the array on the main screen via client-side and i add to it a window that pops up from the main screen, what happens if i click on the child 1 window to pop up a child 2 window, will the main screen now know it has a child of a child open?
No that is not quite possible. I ll get back to you with a probable solution, if my test runs successfully.
ASKER CERTIFIED SOLUTION
Avatar of LeeKowalkowski
LeeKowalkowski
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of coesy

ASKER

talk about pulling it out the hat! thats fantastic, just tried it on our application and it done the job perfectly, thanks very much, really appreciate it