Calling JavaScript function from Flex 4 web application

倖福魔咒の 提交于 2019-12-12 03:57:20

问题


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");
  }


来源:https://stackoverflow.com/questions/3971376/calling-javascript-function-from-flex-4-web-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!