How to close child window when parent window is closed using JavaScript in asp.net application?

In this article I will explain how to close child popup window when parent window is closed using JavaScript in asp.net applications.

Introduction

Sometime we may have a functionality to close child window when parent window is closed in asp.net application. In general we open a child or popup window to display the help information or getting input from user.When we retrieve the input from popup or child window, we block the interaction with parent window until child or popup window is closed but when we display the help information on child window we never block the interactions with parent window, in this case when we close the parent window, child window still open separately, so follow the below simple steps to close the child window in this scenarios.

In the above code we have hyperlink to open child window by using DisplayHelp() javascript method and store the reference to the Popup_Wind array. Now we need to close the child window when parent window closed, by using onunload event in the body we can close the child window. Call the CloseChildWindow() function on onunload event and close all popups by using references which are stored in the array earlier.

That’s it, by using above simple javascript code we can close child windows when parent window is closed also we can use showModalDialog method to open popup windows and this method will not allow to interact parent window until child window is closed. So we can use this method whenever we required instead of using window.open method.

Thanks for reading this article. I hope you enjoyed this article, provide your suggestions and comments.