MyThinkPond

On Java, Python, Groovy, Grails, Spring, Node.js, Linux, Arduino, ARM, Embedded Devices & Web

Importance of named windows and how to close all child windows

Posted by Venkatt Guhesan on January 16, 2011

One of the questions posed by a friend of mine is this.

In a typical website-application, you spawn a few child windows here and there and because of the nature of the application, you do not have a handle to all the child windows you spawn open… Then how do you close all the child windows that belong to your application when the user logs out of your application or closes the main window with the intent that he/she wants to log out?

Well, Javascript “Named” widows to your rescue.

Let’s say you have a parent page/application that has a bunch of links:


<script type="text/javascript">
function openChildWindow(windowID){
	window.open("./WindowOne.html",'MYAPP_'+windowID,'height=400,width=200');
}
</script>

	<a href="Javascript: openChildWindow(1);">Open child window 1</a>.

	<a href="./WindowOne.html" target="MYAPP_2">Open child window 2</a>.

	<a href="Javascript: openChildWindow(3);">Open child window 3</a>.

	<a href="Javascript: openChildWindow(4);">Open child window 4</a>.

	<a href="./WindowOne.html" target="MYAPP_5">Open child window 5</a>.

	<a href="Javascript: openChildWindow(6);">Open child window 6</a>.

	<a href="./WindowOne.html" target="MYAPP_7">Open child window 7</a>.

	Now goto <a href="./secondPage.html">second page</a> which looses all knowledge of these windows...

Now in the secondPage.html

<script type="text/javascript">
function closeAllChildWindows(){
	
	for (var i=0; i<10; i++){
		var childWindowName = "MYAPP_"+i;
		var handle = window.open("",childWindowName);
		if (handle && handle.open && !handle.closed){
			handle.close();
		}
	}
	
}
</script>
<a href="Javascript: closeAllChildWindows();">Close All Child Windows</a>.

Essentially the code looks for all windows with a following convention. In this case, all window-names that start with “MYAPP_*” where *=[0 to 10]. You can do the same trick to see if a window is already opened up under a name so that you can open up a window under a new name but based on your convention.

You can download the sample here.

Cheers!

Advertisements

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

 
%d bloggers like this: