13 Replies Latest reply on Aug 8, 2010 11:17 AM by kglad

    Passing values from swf to html or javascript

    irockska

      Hello everyone.

       

      I am new to flash , i have developed a flash website, just user interface, now i have a contact form in my swf file . can anyone help me in embedding this swf file in html and getting values out of swf file on html page and printing on another html page, i just need help in getting values and out of swf file to html.

       

      thanks and regards

        • 1. Re: Passing values from swf to html or javascript
          kglad Adobe Community Professional & MVP

          you can communicate between flash and javascript using the externalinterface class but i'm not sure that's going to help you.

           

          when you have flash publish your swf and html pages, you can see the code needed to embed your swf in html.  are you having a problem embedding your swf in html?

           

          your users will enter data in a swf form and that data needs to be shared with another html file, correct?  how's that other html file loaded into your browser?  a flash link using getURL or navigateToURL or some other way?

          • 2. Re: Passing values from swf to html or javascript
            irockska Level 1

            hi Kglad

             

            Thanks a lot for quick reply,

             

            I am sorry i think i was not clear in my first post,

            let me explain what i have done and what i have to do.

             

            I am doing a project using jsp and ejb.

             

            i have built a contact for which uses "name", "email" and "user's msg " . and in this form i have a button.

             

            when i publish this page, as you said i got html and swf file, but i dont want html, i want ot embed swf file into jsp page and when it is deployed and after entering data into that contact form, when user presses enter i want the text in those fields to be fetched by my jps page and display in some another swf file which has  a dynamic text filed on which i have to display request number after getting these value.

             

            now basically what i need is to fetch these 3 fields values from swf file and process(which i can do) and send it to another swf file.

             

            thanks again , any help is much appreciated.

             

            Regards

            • 3. Re: Passing values from swf to html or javascript
              kglad Adobe Community Professional & MVP

              to embed your swf you'll need your jsp file to create the embedding code in the flash html page or use the code for a correct swfobject embed.  either way, you need javascript expertise to correctly embed your swf.

               

              to communicate between your two swfs, what is their relationship?  are they both open at the same time?  if so, you can use the localconnection class to communicate data between the two.

               

              if they are open at different times but are both in the same domain and both opened by the same client computer, you can use the sharedobject class.

               

              any other relationshipt between the two will probably require saving the data to your server from the first swf and retrieving the data from the server by the 2nd swf.

              • 4. Re: Passing values from swf to html or javascript
                irockska Level 1

                thanks for your reply.

                 

                i will do the embedding part.

                 

                now what i need is, assume that we have contact.swf which i have embedded in jsp.

                 

                when i run it, and enter the data i need to fetch data from contact.swf  file and store it in data base (i can do database part).

                 

                now in mean while on user screen there will be a button "continue" after pressing that button user will be redirected to another swf file called thankyou.swf where  "request number" ( which is created by processing name and email.) is printed with thank you msg.

                 

                I can embed these two file , these are in same domain, my main concern is passing data from swf to jsp and jsp to swf.

                 

                thanks

                • 5. Re: Passing values from swf to html or javascript
                  kglad Adobe Community Professional & MVP

                  there's no need to save the file in a database (unless you need it for something else).  use the sharedobject.

                  1 person found this helpful
                  • 6. Re: Passing values from swf to html or javascript
                    irockska Level 1

                    thanks a lot,

                     

                    any other suggestions will be greately appreaciated.

                    • 7. Re: Passing values from swf to html or javascript
                      kglad Adobe Community Professional & MVP

                      i'd recommend using swfobject to embed your swf.  it's much cleaner to dynamically generate that code.

                      1 person found this helpful
                      • 8. Re: Passing values from swf to html or javascript
                        irockska Level 1

                        thanks a lot kglad.

                         

                        i dont have problem in embedding swf , its only transfering of values.

                         

                        thanks again for all your help

                        • 9. Re: Passing values from swf to html or javascript
                          kglad Adobe Community Professional & MVP

                          you're welcome.

                           

                          using the sharedobject will eliminate that problem with getting data from one swf to another for the same client using swfs in the same domain.

                          • 10. Re: Passing values from swf to html or javascript
                            irockska Level 1

                            thanks a lot ,  with shared objects i am getting some where. i got this code from the internet. i understood the action script part. but i did not understand what javascript is doing here.

                             

                            look into the color coded section , what does the (movieName) refers to

                             

                            The following example demonstrates sending data between Flash Player and an HTML container.

                            package {
                                import flash.display.Sprite;
                                import flash.events.*;
                                import flash.external.ExternalInterface;
                                import flash.text.TextField;
                                import flash.utils.Timer;
                                import flash.text.TextFieldType;
                                import flash.text.TextFieldAutoSize;

                                public class ExternalInterfaceExample extends Sprite {
                                    private var input:TextField;
                                    private var output:TextField;
                                    private var sendBtn:Sprite;

                                    public function ExternalInterfaceExample() {
                                        input = new TextField();
                                        input.type = TextFieldType.INPUT;
                                        input.background = true;
                                        input.border = true;
                                        input.width = 350;
                                        input.height = 18;
                                        addChild(input);

                                        sendBtn = new Sprite();
                                        sendBtn.mouseEnabled = true;
                                        sendBtn.x = input.width + 10;
                                        sendBtn.graphics.beginFill(0xCCCCCC);
                                        sendBtn.graphics.drawRoundRect(0, 0, 80, 18, 10, 10);
                                        sendBtn.graphics.endFill();
                                        sendBtn.addEventListener(MouseEvent.CLICK, clickHandler);
                                        addChild(sendBtn);

                                        output = new TextField();
                                        output.y = 25;
                                        output.width = 450;
                                        output.height = 325;
                                        output.multiline = true;
                                        output.wordWrap = true;
                                        output.border = true;
                                        output.text = "Initializing...\n";
                                        addChild(output);

                                        if (ExternalInterface.available) {
                                            try {
                                                output.appendText("Adding callback...\n");
                                                ExternalInterface.addCallback("sendToActionScript", receivedFromJavaScript);
                                                if (checkJavaScriptReady()) {
                                                    output.appendText("JavaScript is ready.\n");
                                                } else {
                                                    output.appendText("JavaScript is not ready, creating timer.\n");
                                                    var readyTimer:Timer = new Timer(100, 0);
                                                    readyTimer.addEventListener(TimerEvent.TIMER, timerHandler);
                                                    readyTimer.start();
                                                }
                                            } catch (error:SecurityError) {
                                                output.appendText("A SecurityError occurred: " + error.message + "\n");
                                            } catch (error:Error) {
                                                output.appendText("An Error occurred: " + error.message + "\n");
                                            }
                                        } else {
                                            output.appendText("External interface is not available for this container.");
                                        }
                                    }
                                    private function receivedFromJavaScript(value:String):void {
                                        output.appendText("JavaScript says: " + value + "\n");
                                    }
                                    private function checkJavaScriptReady():Boolean {
                                        var isReady:Boolean = ExternalInterface.call("isReady");
                                        return isReady;
                                    }
                                    private function timerHandler(event:TimerEvent):void {
                                        output.appendText("Checking JavaScript status...\n");
                                        var isReady:Boolean = checkJavaScriptReady();
                                        if (isReady) {
                                            output.appendText("JavaScript is ready.\n");
                                            Timer(event.target).stop();
                                        }
                                    }
                                    private function clickHandler(event:MouseEvent):void {
                                        if (ExternalInterface.available) {
                                            ExternalInterface.call("sendToJavaScript", input.text);
                                        }
                                    }
                                }
                            }

                            In order to test the previous ActionScript code, embed the generated SWF file using the following HTML template:

                             <!-- saved from url=(0014)about:internet -->
                            <html lang="en">
                            <head>
                            <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
                            <title>ExternalInterfaceExample</title>
                            <script language="JavaScript">
                                 var jsReady = false;
                                 function isReady() {
                                     return jsReady;
                                 }
                                 function pageInit() {
                                     jsReady = true;
                                     document.forms["form1"].output.value += "\n" + "JavaScript is ready.\n";
                                 }
                                function thisMovie(movieName) {
                                     if (navigator.appName.indexOf("Microsoft") != -1) {
                                         return window[movieName];
                                     } else {
                                         return document[movieName];
                                     }

                                 }

                                 function sendToActionScript(value) {
                                     thisMovie("ExternalInterfaceExample").sendToActionScript(value);
                                 }
                                 function sendToJavaScript(value) {
                                     document.forms["form1"].output.value += "ActionScript says: " + value + "\n";
                                 }
                            </script>
                            </head>
                            <body onload="pageInit();">

                                 <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
                                         id="ExternalInterfaceExample" width="500" height="375"
                                         codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab">
                                     <param name="movie" value="ExternalInterfaceExample.swf" />
                                     <param name="quality" value="high" />
                                     <param name="bgcolor" value="#869ca7" />
                                     <param name="allowScriptAccess" value="sameDomain" />
                                     <embed src="ExternalInterfaceExample.swf" quality="high" bgcolor="#869ca7"
                                         width="500" height="375" name="ExternalInterfaceExample" align="middle"
                                         play="true" loop="false" quality="high" allowScriptAccess="sameDomain"
                                         type="application/x-shockwave-flash"
                                         pluginspage="http://www.macromedia.com/go/getflashplayer">
                                     </embed>
                                 </object>

                                 <form name="form1" onsubmit="return false;">
                                     <input type="text" name="input" value="" />
                                     <input type="button" value="Send" onclick="sendToActionScript(this.form.input.value);" /><br />
                                     <textarea cols="60" rows="20" name="output" readonly="true">Initializing...</textarea>
                                 </form>

                            </body>
                            </html>



                            thanks
                            • 11. Re: Passing values from swf to html or javascript
                              kglad Adobe Community Professional & MVP

                              that code is used for the localconnection class when you want to exchanged data between actionscript and javascript.  you have no need for that (unless you need it for something else).

                              • 12. Re: Passing values from swf to html or javascript
                                irockska Level 1

                                yes i need it for something else, i have been trying some other stuff too.

                                 

                                anyways thanks for all your help.

                                • 13. Re: Passing values from swf to html or javascript
                                  kglad Adobe Community Professional & MVP

                                  in that case the code in red should not be changed by you.  it is used by flash to return a reference to the html document.

                                   

                                  however, you should change ExternalInterfaceExample below to match your swf's name (minus the dot swf extension):

                                   

                                   

                                  function sendToActionScript(value) {
                                           thisMovie("ExternalInterfaceExample").sendToActionScript(value);
                                       }