www.webdeveloper.com
Results 1 to 4 of 4

Thread: Closing all Child windows when Parent window is closed !!

  1. #1
    Join Date
    Apr 2009
    Posts
    2

    Closing all Child windows when Parent window is closed !!

    Hi guys,

    I am trying to close all child windows when I close my parent window. First of all, I thought of doing this during page onunload event. But my application in asp.net will do many postbacks and everytime the onunload is called and hence my javascript array will be reinitialised everytime. So, I came with another approach where in I am storing the object in a hidden control at server side. So, when the child windows are open, i am adding the objects to the hidden control by appending a delimiter "~".

    When the parent is closed, I am fetching this array to javascript and then looping through the array and trying to close al child windows. But it is not working. I am very much hopefull that it should work since alert(array[i]) is printing [object].

    here's the code....please help me...

    //conf() function is called when a button is clicked to open the child window.
    function conf()
    {

    var rand_no_c = Math.random();
    var con = rand_no_c.toString();
    var_win = 'child'+con.substring(2)

    var var2 = window.open('child.aspx',var_win,
    'width=1100,height=700,left=50,top=20,status=0');

    document.getElementById('hdnDummy').value = var2;


    }

    //The below function is called when I close my parent window.

    window.onbeforeunload= function closewindow()
    {
    if((window.event.clientX<0) || (window.event.clientY<0))
    {

    var listArray = new Array();
    var listString = document.getElementById('hdnDummy1').value;

    // here hdnDummy1 = hdnDummy1 + hdnDummy + "~" at server side.

    listArray = listString.split('~');

    for(var i = 0; i < listArray.length-1; i++) {
    alert(listArray[i]); // this is working fine
    listArray[i].close(); // this is not working....

    }

    Please help me guys.....

  2. #2
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en">
    <head>
    <title>window manager</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    
    <script type="text/javascript">
    var windows = [];
    function conf() {
    var rand_no_c = Math.random();
    var con = rand_no_c.toString();
    var_win = 'child'+con.substring(2);
    windows[var_win] = window.open('child.aspx',var_win,'width=1100,height=700,left=50,top=20,status=0');
    }
    
    function closewindow() {
    for(w in windows) {
        if(!windows[w].closed) {
            windows[w].close();
            }
        }
    }
    </script>
    
    <style type="text/css">
    * {margin:0;padding:0;}
    </style>
    
    </head>
    <body>
    <div>
        <button type="button" onclick="conf();">open child window</button>
        <button type="button" onclick="closewindow();">close all windows</button>
    </div>
    </body>
    </html>
    At least 98% of internet users' DNA is identical to that of chimpanzees

  3. #3
    Join Date
    Apr 2009
    Posts
    2

    Closing all Child windows when Parent window is closed !!

    Thanks Fang for the reply. But, this will work when there is no post back happens. If i code this logic, whenever my page does a postback, the array var windows = []; will be reinitialised and the contents of the array are lost everytime. Hence I came up with the approach of storing the window.open objects into c# array and then fetch the entire array when the close button of the parent is clicked. My code mentioned in the post is able to do this, but only thing is when i loop through the javascript array and then do array[i].close(), it is not working. Need your help if u could try this code in your page and let me know how could i make this working. thanks mate.

  4. #4
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    Once the parent window reloads the connection to child windows is lost, even if you know their names.
    At least 98% of internet users' DNA is identical to that of chimpanzees

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles