how to pass value from java script to embedded flex object?

1.6k views Asked by At

I am trying to figure how to pass string value(url) from html form to embedded flex object. the only method i found so far is "addCallback" method described in http://livedocs.adobe.com/flex/3/html/help.html?content=passingarguments_5.html In the example i used flex function "myFunc(s:String)" is registered with "ExternalInterface" and called later from javascript

--->mySwf.mxml:

<?xml version="1.0"?>
<!-- wrapper/AddCallbackExample.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="initApp()">
  <mx:Script>
     import flash.external.*;
      import mx.controls.Alert;
     public function initApp():void {
        ExternalInterface.addCallback("myFlexFunction",myFunc);
     }  
     public function myFunc(s:String):void {

         Alert.show(s, 'Alert Box', mx.controls.Alert.OK);
     }

  </mx:Script>
  <mx:Button id="myButton" 
        label="FLEX BUTTON" 
        click="Alert.show('FLEX LOADED!', 'Alert Box', mx.controls.Alert.OK);"/>
  <mx:Label id="l1"/>

</mx:Application>

external.html

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<SCRIPT LANGUAGE="JavaScript">
    function callApp() {

        mySwf.myFlexFunction("show me something");
    }
</SCRIPT>


<form id="f1">
    <button onClick="callApp()">HTML BUTTON</button>
</form>

<OBJECT CLASSID="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
WIDTH="850"
HEIGHT="610"
CODEBASE="http://active.macromedia.com/flash5/cabs/swflash.cab#version=5,0,0,0">
<EMBED SRC="mySwf.swf"
WIDTH="850"
HEIGHT="610"
PLAY="true" 
LOOP="true"
QUALITY="high" 
scale="noborder"
PLUGINSPAGE="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"> 
</EMBED>
</OBJECT>
</html>

The method doesn't seem to be working at all. If I push Flex button - i can see Flex popup dialog. When I push HTML button, calling myFunc in Flex via ExternalInterface - nothing
happens... Any pointers to errors in my code? Thank you,

1

There are 1 answers

12
KJYe.Name On BEST ANSWER

Try this in case you aren't using the right browser:

<SCRIPT LANGUAGE="JavaScript">
    // This function returns the appropriate reference, 
    // depending on the browser.
    function getFlexApp(appName)
    {
      if (navigator.appName.indexOf ("Microsoft") !=-1)
      {
        return window[appName];
      } 
      else 
      {
        return document[appName];
      }
    }

    function callApp() {
        getFlexApp('mySwf').myFunc("show me something");
    }

</SCRIPT>

also you did not give an id to your object which is how the DOM identifies

<OBJECT id='mySwf' CLASSID="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000">

try this:

ExternalInterface.addCallback("myFunc",myFunc);