8 Replies Latest reply on Jun 16, 2009 5:35 AM by robdillon

    CS3 / AS.20: Closing the browser window from Flash?

    davebartlett

      Hopefully someone out there will have an answer for me, as I'm pulling my hair out!

       

      I am currently trying to work out how to get Flash to close the browser window it is running in.  I've done a simple Javascript test version which works fine.. but if I try and used the ExternalInterface.call("closeMyWindow"); functionality it does not.

       

      My simple Javascript version is cut and paste below and attached to this message.

       

      If anyone has any ideas, then any feedback will be appreciated.. !

       

      Dave

       

      ---

       

      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      <html>

           <head>

                 <title>Untitled</title>
                 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
                 <meta name="language" content="en" />
                 <meta name="description" content="" />
                 <meta name="keywords" content="" />

                 <script language="javascript">
       
                       function closeMyWindow() {
                             window.open('',"_self",'');
                             window.close();
                       }
       
                 </script>

           </head>

       

           <body>
                 <input type="button" id="closeMe" value="Press Me!" onclick="closeMyWindow()"/></input>

           </body>


      </html>

        • 1. Re: CS3 / AS.20: Closing the browser window from Flash?
          kglad Adobe Community Professional & MVP

          javascript can only close a window that's been opened using javascript and it's not clear what window you want to close.

          • 2. Re: CS3 / AS2.0: Closing the browser window from Flash?
            davebartlett Level 1

            I'm am trying to close the browser window the Flash movie is embedded - so in other words, the HTML page that is published from Flash in the same location as the SWF file.

             

            Javascript can close other windows, but you need to use a "workaround", which is what the Javascript listing I pasted with my previous post does.It opens a null window in the current HTML page and then closes it.  Another work round that works fine in normal HTML / Javascript is to have the following: the parent.html is replaced by the close.html when the button is clicked, and the HTML page then closes itselfs because a dummy "new window" is made and then told to close.

             

            However, once I try calling a function such as closeWindow from Flash using ExternalInterface, it all stops working.

             

            File: parent.html

            ============

             

            <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
            <html>
            <head>
            <title>Parent Window</title>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
            <meta name="language" content="en" />
            <meta name="description" content="" />
            <meta name="keywords" content="" />
            <script language="javascript">
             
              function closeWindow() {
               window.open("close.html", "_self");
              }
             
            </script>
            </head>

            <body>

            <h1>Parent Window</h1>
            <p>Press the button to close this window.</p>
            <input type="button" id="closebutton" value="Press Me!" onclick="closeWindow()"/></input>

            </body>

            </html>

             

            ---

             

            File:close.html

             

            <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
            <html>
            <head>
            <title>Untitled</title>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
            <meta name="language" content="en" />
            <meta name="description" content="" />
            <meta name="keywords" content="" />
            </head>

            <body>

            <script language="javascript">
             
              window.close();
             
            </script>

            </body>


            </html>

            • 3. Re: CS3 / AS2.0: Closing the browser window from Flash?
              robdillon Most Valuable Participant

              Have you tried adding an alert to the javascript function to see it is being called from the Flash file? Flash doesn't care what the JS function does, it just issues the call, so I'm guessing that the problem lies in how you are using externalInterface.

              • 4. Re: CS3 / AS2.0: Closing the browser window from Flash?
                kglad Adobe Community Professional & MVP

                that doesn't look like it's going to close a window.  it's just going to load close.html and close.html isn't going to close itself.

                • 5. Re: CS3 / AS2.0: Closing the browser window from Flash?
                  davebartlett Level 1

                  Yeah,I've tried that.. the function is called ok.. Well, as far as any alert() message.. It just seems to be anything to do with the window.close() functionality that does work when called by Flash.

                  • 6. Re: CS3 / AS2.0: Closing the browser window from Flash?
                    davebartlett Level 1

                    Actually it does work...  You are tricking IE into thinking that the current window is a new popup, and then loading into the "dummy"  window the content of close.html, which contains the Javascript instruction to close the window.

                     

                    It does work because.. well, in the process of trying to get Flash to do the same... I've run the pages through IE lots of times .. and its closed..

                    We are targeting IE7, so it doesn't really matter if it doesn't work in Firefox, since the client is not allowed to run or install additional software.

                     

                    Flash seems to struggle - maybe with some kind of annoying security setting - to execute anything regarding window.open or window.close when referencing the current browser window.

                    • 7. Re: CS3 / AS2.0: Closing the browser window from Flash?
                      davebartlett Level 1

                      A bit more information to clear things up...   Since it's even giving me a headache.

                       

                      The AS2.0 I've got in the Flash movie is as follows:

                       

                      import flash.external.ExternalInterface;
                      var functionCall_str: String = new String();
                      this.stop();

                       

                      // close_btn: Makes external call to JS in the HTML
                      close_btn.onRelease = function() {
                            trace("Pressed 'close_btn' button to close browser..");
                            functionCall_str = String(ExternalInterface.call("closeWindow"));
                      }

                       

                      In the Flash published HTML file, I've added this function within the <script> tag in the body:

                       

                      function closeWindow() {
                           alert("Called closeWindow()");
                           window.open("close.html", "_self");
                      }

                       

                      When you press the button in the Flash movie, the alert() works ok:

                       

                      alert.png

                      • 8. Re: CS3 / AS2.0: Closing the browser window from Flash?
                        robdillon Most Valuable Participant

                        If the JS function is called from the Flash movie, then the problem is with javascript. Since you are using a non-standard JS method in IE, I'm guessing that either the implementation of JS in IE is unreliable in this instance or the method is unreliable.