Calling JavaScript function from Flex 4 web applic

2019-09-06 19:23发布

问题:

I need to call javascript function from Flash 4 based web application. When I run it in Debug mode it runs perfectly but when I make release build or run same application on other machine it does not call JavaScript function.

For testing I am just calling sample Alert function of JavaScript. Can someone help me what I am missing ?

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
    layout="vertical" initialize="application1_initializeHandler(event)"
    verticalAlign="middle"
    backgroundColor="white">

 <mx:Script>
  <![CDATA[
   import mx.controls.Alert;
   import mx.events.FlexEvent;
   public function btnLogin_click():void 
   {
    var s:String;    
    if (ExternalInterface.available) 
    {     
         ExternalInterface.call("alert", "Hello World!"); 
    } 
    else 
    {
     Alert.show("External interface not available");
    }
    trace(s); 
   }

   protected function application1_initializeHandler(event:FlexEvent):void
   {
    flash.system.Security.allowDomain("always");
   }

  ]]>
 </mx:Script>

 <mx:Form>       
  <mx:FormItem>        
   <mx:Button id="btnLogin" label="Login" click="btnLogin_click()" />        
  </mx:FormItem>       
 </mx:Form>

</mx:Application>

回答1:

Well, firstly, make sure JavaScript on your testing machine is turned on and then also make sure you are adding your JavaScript file/code after adding swfobject.js file.

I had similar problem but it worked out when I moved swfobject.js at the top of all js includes.



回答2:

Have you tried a test like (Flex):

ExternalInterface.call("alertFn");

And JS:

function alertFn() {
    alert("hello world");
}

?

I've never tried an ExternalInterface call to a native JS function like "alert"...



回答3:

After digging out the error code 2060 through

Alert.show(e.message)

I figured out that for some reason ExternalInterface.call doesn't work on a file:// and needs http(s)://

So, anybody who is facing this problem, get your yourself a webserver(Apache) or a GAE for testing these kinds of things and save yourself from the "Extreme time wastage":

I was having endless hours of problems using file:// with the Flex AJAX Bridge.

The AJAX code would fail silently during the SWF initialization callbacks to the AJAX code. I would then have null values for all of the SWF root elements.

As soon as I installed a web server and started using http:// localhost everything worked perfectly.

Extreme time wastage :(



回答4:

test.mxml ..........

     protected function bt1_clickHandler(event:MouseEvent):void
        {

            // TODO Auto-generated method stub
            ExternalInterface.call("callUnity");
        }

.js ....

 function callflex(){

 alert("got it");
  }