Jump to content

open


user4fun

Recommended Posts

I got this code form the site,

<script type="text/javascript">function open_win() {window.open("http://www.microsoft.com/")window.open("http://www.w3schools.com/")}</script></head><body><form><input type=button value="Open Windows" onclick="open_win()"></form>

What would i need to put in there to have a ntoher button that would close all the windows that were just opened.?Thank you

Link to comment
Share on other sites

Two things would need to happen. The first is that you would need to record a reference to those windows so that you could close them at some other time. The second would be to write the function.So, change the open_win function to something like this:

// first, we need a global reference to the windowsvar win1, win2;function open_win(){	// rather than just opening the windows, open the windows 	// and save a reference to the windows in our variables	win1 = window.open("http://www.microsoft.com/");	win2 = window.open("http://www.w3schools.com/");}

Then, write a close_win function:

function close_win(){	// first, check to see that the win1 variable	// is defined and then check to see if it is open	if(win1 && win1.closed == false)	{		// everything's good, close the window		win1.close();	}	// repeat the steps above for win2.	if(win2 && win2.closed == false)	{		win2.close();	}}

You could then call the function with a new button:

<input type="button" value="Close Windows" onclick="close_win();" />

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...