How to close all child windows on closing/logout of Main page/window (Asp.net web application)

In many web portal applications, there are lots of popup windows get opened when users navigate to some links or there may be some popups window providing some relevant information. These popups may get opened when some specific events occur on the site or may be opened by user’s click.

In this flow of site, at a particular time there can be lots of child windows/popups opened and user may not want to close these individual windows when user wants to move out of the site either by signing off/logging off or by just closing the main window/page.

So to close all those child windows we need to handle the events fired when main window gets closed i.e. unloaded or when user clicks on the LogOut button.

But to achieve this, first we need to store the child windows’ identity  so that they can be closed at the required time.

Lets consider that there are two popup windows get opened at some particular events or instances. When these windows are opened using window.open() they must be stored in some variable.                                                          Suppose I am calling a javascript funtion on the page load of the Main window which will open two new windows (page1, page2).

protected void Page_Load(object sender, EventArgs e)

    {

        if (!IsPostBack)

        {

          this.Page.RegisterStartupScript(“openchildwindows”,   “<script>OpenWindows();</script>”);

        }

    }

And in the Javascript:

 

    var winArray = new Array();

    function OpenWindows()

    {        

        winArray[0] = window.open(“page1.aspx”, “Page1”);

        winArray[1] = window.open(“page2.aspx”, “Page2”);

    }

As Shown, windows are stored in an array. Now suppose we have a button on the main page ‘LogOut‘ and there is an event handler for click event of this button. We will call a javascript funtion ‘CloseWindows‘ on the page unload as well as on click of the logout button. This function will close all the child windows as per the stored array.

Define OnUnload event of the main page as:

<body onunload=”CloseWindows()“>

And in the click event of the LogOut button:

//do your operation on loggin off

this.Page.RegisterStartupScript(“openwindows”, “<script>CloseWindows();</script>”);  

JavaScript funtion:

function CloseWindows()

{    

        winArray[0].close();

        winArray[1].close();

}

This will close all the child windows.

Advertisements
This entry was posted in .Net 2.0. Bookmark the permalink.

5 Responses to How to close all child windows on closing/logout of Main page/window (Asp.net web application)

  1. Munish says:

    We can also close the parent/main window using the javascript:

    this.window.close();

    And, we can keep track of the child windows, if they all get closed then we can close the parent window; as pe the requirement.

    This can be done by
    1. Pass the window’s Id to the child window being opened from the parent window.
    2. Call a javascript function of the parent window (using window.opener.Function()) and pass back this same window’s ID.
    3. In the javascript function being called of the parent page, use this window’s ID and close the child window.

  2. Alexwebmaster says:

    Hello webmaster
    I would like to share with you a link to your site
    write me here preonrelt@mail.ru

  3. Pingback: How to close all windows upon signout? | Arnold Sia's space

  4. Bennie says:

    Very quickly this web site will be famous among all blogging and site-building users,
    due to it’s pleasant articles

  5. Hey there! This is my first comment here so I just wanted to give a quick shout out and tell
    you I really enjoy reading through your blog posts. Can you recommend any other blogs/websites/forums that deal with the same topics?

    Appreciate it!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s