The Linux Page

Closing a window from Flash

I had a little project which requirements was an Adobe Flash animation that calls a JavaScript function to close a browser window.

With the newest version of Internet Explorer, it will first ask you whether you want to let the script close your window... that's a problem when you'd like that process to be smooth (i.e. without user interaction.)

I found many pages with information about getting Flash to send an FSCommand. One of those pages is a 1999 sample that still works: FS Command: A Short Introduction. That script uses the Get URL flash command with the URL set to "FSCommand:call_alert" and the target set to "Hello World!". If you need to use dynamic parameters, use the Get URL2 instead.

As for the JavaScript code, you just need to define a function which name starts with the name of your movie and ends with _DoFSCommand(). This way, you may have multiple Flash animations in a single page and still handle each one's FS Command calls.

For Internet Explorer to work, especially older versions, you also want to include a VB Script to catch the FS Command calls. That function goes like this:

  <script type="text/vbscript">
    Sub myMovie_FSCommand(ByVal command, ByVal args)
      Call myMovie_DoFSCommand(command, args)
    End Sub
    // -->

Just like with the JavaScript DoFSCommand function name, you must prepend the name of your movie in front of the VB Script FSCommand function. There is a sample of the corresponding JavaScript code:

  <script type="text/javascript">
    function myMovie_DoFSCommand(command, args)
      if(command == 'my_command') {
        ... // code executing 'my_command' in JavaScript

Now, how do you close the window in JavaScript? Very simple, there is one command line:


Unfortunately, that does not work fabulously... Quite often the browser will either not close the window (totally ignore the command) or it will ask the user whether he/she wants to close the window. So, if your intent was to hide what is visible in the window, you will want to first hide everything... (maybe even make the document empty.) Setting the display style to none is one way, but if your content is just text, remember that users can still go check out the source of your page and read the whole thing. Doing InnerHTML = ''; is much more powerful in this case.

For Internet Explorer, some people said you mark the window as a self opener before calling the close() function. Yet that does not seem to work...

  window.opener = self;

With IE 8.x it does not make any difference. Even though the window was opened with a link including a target="_blank" in it.

The real problem is the way the window is opened and not the way it is to be closed. You MUST open the window with a JavaScript snippet or you cannot close the window with a JavaScript snippet and not get an alert from Internet Explorer.

So, you will want to have something such as:'<url>', '<window name>', '<parameters>');

Note that the <window name> is a machine name, so it can include letters, digits and underscores. The name must not start with a digit.

The <parameters> are defined as <name>=<value>,... where <name> can be one of those:

  width, height, screenX, screenY, left, top, resizable, scrollbars,
  toolbar, location, directories, status, menubar, copyhistory

The URL can be a local or full URL.

By the way, all of this works on FireFox as well as IE.

Note that there is another solution which is to call the window.close() function directly from your Flash animation. That generally works great because it does not look like they have as many checks about how the window was opened.

For the JavaScript order to work from within your Flash animation, you want to use geturl() with the URL set to javascript:window.close(); and the target set to _self. Without that target, it is not likely to work right.

SMS From Me Logo

SMS From Me

To automatically start one on one conversations with your online leads.