1

I have been searching high and low for a solution to my problem and can only find pieces of the puzzle with no way to connect them. I have a window (which it self is a child of the main window) that can open multiple other windows. I want the children windows to be closed when the parent is closed, sounds simple enough right?

Here is the dilemma, my parent window does a postback when opening the child so any array of handles (which are also objects) I keep does not persist. I have looked into serializing and de-serializing this array and storing it in a hidden field but that doesn't seem like the best solution. I also can not access the parent of the window doing the opening.

Aside from Javascript, the server side code is written in C# and I'm using asp.net. Is there any reasonable solution to this? I should also mention that the code I am working with was written by multiple different people long before I got to it so I would like to add to it rather than changing how most of it works.

Finally, I know it is typically a good idea to post my code on here but I am simply using var win = window.open to perform the window opening task.

Thank you.

MGraffia
  • 13
  • 3
  • 2
    I'd suggest using modal style popups for this (like jquery dialogues). Most browsers close windows anyway and you can't climb back up the window tree which means if you have 5 open and you close window 2 from window 1, then window's 3, 4, and 5 will remain open. – Gats Mar 15 '12 at 16:48
  • I can look into doing this, my biggest problem is that I am not supposed to be changing the way things currently work, just adding functionality. – MGraffia Mar 15 '12 at 16:56

2 Answers2

0

What about

var wins = [];
function newChildWindow(...) {
    wins.push(window.open(...));
}
window.onuload = function() {
    for (var i=0; i<wins.length; i++)
        if (!wins[i].closed)
            wins[i].close();
};
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • The problem with this is that the array of window objects does not persist to the point where I need them. – MGraffia Mar 15 '12 at 16:54
  • Why would it not persist? The array will persist until the window is closed, won't it? – Bergi Mar 15 '12 at 16:59
  • The parent window does a postback which refreshes it, resetting any javascript variables. I have tried to store the object in the viewstate or in a hidden field but it needs to be serialized. – MGraffia Mar 15 '12 at 17:09
  • ah, allright. Didn't read that carefully enough. See my other answer then – Bergi Mar 15 '12 at 17:34
0

OK, I then see 3 ways to get around that problem:

  • Don't use multiple browser windows, but style your page to show all the dialogues; as @Gats suggested. OK, that will somewhat limit you if you want the input of parent "windows" persistant while navigating in a child window.
  • Don't do a refresh postback to unload the parent. Use Ajax instead to send data to the application server
  • You can't store (window) objects, you only can store strings. When opening a window, you can give it a "target" name, and any other window.open with the same target (name) will use the already open one. With that, you can reference windows you didn't open yourself - and close them. See https://stackoverflow.com/a/563478/1048572
Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • The third option seems like something I might be able to use but doesn't the name have to be something like "_blank" or "_self"? I know I can open multiple windows with the same url and a name of "_blank". – MGraffia Mar 15 '12 at 19:20
  • naa, different urls and names like "modal-dialogoue-child-1" (and so on), so that you can tell them apart – Bergi Mar 15 '12 at 19:49
  • I used the third option along with some server side code and finally got this working. I appreciate all of the help. – MGraffia Mar 15 '12 at 21:54