MACROMEDIA FLASH  back to
CLOSING A WINDOW  


closing a window

description:
once you have created a secondary window, you will eventually want the user to be able to close it and return to the main window or leave the site. thankfully closing a window is easier than opening one. but there's still an important issue: how do we send commands from flash to javascript without excluding some browsers? the safest approach is to call a normal html page into the browser, and let that page initiate some javascript to do the window closing.

how to
to close a window you only need to do 2 things: 1) make a window closing page, 2) link to the window closing page. here's how:

step 1) first, we make a page which, when loaded, runs a window-closing javascript function. here's the code:
<HTML>
<HEAD><TITLE>Window Closer</TITLE></HEAD>
<BODY ONLOAD="top.close()">
</BODY>
</HTML>
the "onload" attribute of the BODY tag is a javascript event handler that executes the code in quotes when the page loads. The "top.close()" code tells the browser to perform the "close()" function on the current browser window ("top"), which closes the window.

copy the above code and save it as window-closer.html.
step 2) whenever you want to close the window, just link to the window-closer.html page using flash's getURL() function on a button, movie clip or frame, like this:

getURL ("window-closer.html");

you can even just use a standard html anchor tag (eg. < A HREF="window-closer.html">).

i told you it was easier to close a window than to open one...try it out below:

faq: why is the browser asking me if i want to close the window?
if you are getting a message that says something like "are you sure you want to close this window?" when you execute your close window command, you are trying to close the *main* browser window, not a window that you've popped up. any time you try to close the main browser window with javascript, the browser will ask the user that question. there's no way to get rid of it 'cause otherwise a malicious site could use the window closer to shut down the user's browser without their consent. you can only silently close windows you've opened yourself with javascript.

revision history
late 1998: posted.
october 29, 1999: added the faq about closing the main browser window, and generally cleaned the page up a bit.
sept 26, 2000: changed self.close() to top.close() so the closer works in a frameset.