[Back to previous page] [Next Page in Series]

Ken Ward's Java Script Tutorial ...

Example of windows closing on exiting the page.

The windows on this page may overlap, so separate them. Press the browser's back button for this page and the windows will all go! Press the back button now and look for those little windows. I hope you can't find them! Then come back and look at the code.

First of all, we need to declare two variables, win1 and win2:

<script type="text/javascript">

<!--

var win1,win2;

.We need to do this because the variables in the first function is not visible to the other function. So we declare it globally.

Next we write a function to open two windows in the HEAD of the document:

function LoadWindows()

{

win1=window.open('','Window1','width=200,height=200');

win2=window.open('','Window2','width=100, height=100');

win1.document.write('This is window one');

win2.document.write('This is window two');

}

We create two variables, win1 and win2 globally. We do not load an existing window so the first two single quotes ('') are empty (but must be there). The next bit is Window1, which is not really necessary and could be the same as before - two single quotes (''), so JavaScript could please itself, for all we care! As we do not mention anything about the window being resizable, etc, we do not get a resizable window! We use the variables win1, and win2, to write to the window our exciting messages.

We activate this part of the script in the BODY tags by using:

onLoad="javascript:LoadWindows()"

When we close this page, we also close all the windows we have opened. We do this by writing a function in the HEAD:

function UnLoadWindows()

/*This function closes all the windows when

the page closes*/

{

win1.window.close();

win2.window.close();

}

//-->

</script>

We close our windows by using the window.close() function. If we just say 'window.close()' then the Browser will get involved and ask us if we really want to close down. This function was put in this page's BODY:

onUnload="javascript:UnLoadWindows();return false"

 

[back to:  ]
[Contents of this Site][contents of Java Script Tutorial]
[on to: ]
[If you need to learn more about HTML visit the HTML Tutorial]
 

Most Recent Revision: 18-Oct-98.
Copyright 1998

I am always pleased to hear from you.
Send your comments to
and please visit: The New Life Course