Java Script: Closing multiple child windows on parent window closing or refreshing

In my previous article (Java Script: Closing child window or tab on parent window closing or refreshing) I gave the sample source code for closing single child window on closing or refreshing parent window. In this article I am providing a sample java script code for closing multiple child windows. For closing multiple child windows, the window object should be assigned to an array variable  which is common to both the opening and closing function. While closing the window the array should be iterated using for loop and close all the child windows.

Java Script closing multiple child windows

Sample Source Code in the parent page for closing multiple child windows:

<html>
<head>
<title>www.MyTecBits.com</title>
<script type="text/javascript">
<!--
 var mtbChildWin = new Array();
 var mtbWinCound = 0;
function LaunchMyTecBitsWindow(IDParam, FileExtension, isClassicDocument) {
 var urlstring = "mtbTest.htm";
 mtbChildWin[mtbWinCound] = window.open(urlstring, mtbWinCound);
 mtbWinCound++;
 return false;
 }
function CloseMyTecBitsWindow() {
 for (mtbWinCound = 0; mtbWinCound < mtbChildWin.length; mtbWinCound++) {
 if (mtbChildWin[mtbWinCound] != null && !mtbChildWin[mtbWinCound].closed)
 mtbChildWin[mtbWinCound].close();
 }
 }
-->
</script>
</head>
<body onunload="CloseMyTecBitsWindow();">
 <input type=button value="Open Child" onclick="LaunchMyTecBitsWindow();"/>
 <br />
</body>
</html>

For closing only a single child window read my other article (Java Script: Closing child window or tab on parent window closing or refreshing).

 


Leave your thoughts...

This site uses Akismet to reduce spam. Learn how your comment data is processed.