0

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>
0

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

9 Comments

Thanks Faheem, I am running both debug/release code on same machine. And Debug mode able to call JavaScript and Release mode not able to call. Actually for testing I am not adding any JS file. Just calling native JavaScript method "alert" with a parameter. But still not running. If someone can reproduce same problem then It will be really helpful.
Why don't you try the sample code above on some fresh machine that doesn't have debugging tools on it. I have tried your sample code in both release and build mode, working fine.
I am getting following error :SecurityError: Error #2060: Security sandbox violation: ExternalInterface caller file:///C:/MSAPerMonWebClient/bin-release/MSAPerMonWebClient.swf cannot access file:///C:/MSAPerMonWebClient/bin-release/MSAPerMonWebClient.html. at flash.external::ExternalInterface$/_initJS() at flash.external::ExternalInterface$/call() at MSAPerMonWebClient/btnLogin_click() at MSAPerMonWebClient/__btnLogin_click()
Have you tried addCallBack method? Also check - throws - security error 2 solution here livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/…
|
1

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 Comments

Well, The code I have written is working in Debug mode. But when I am making release build it not calling JS. Seems like some security issue. Can you guys run above code in Flex 4 builder and validate it ?
Can anyone one send me working project calling JavaScript function with full code ?
@FlexJogger, its not the security issue thing. When you release build, your javascript is overwritten by the new HTML wrapper. You need to write your JS again.
0

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 :(

Comments

0

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

     protected function bt1_clickHandler(event:MouseEvent):void
        {

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

.js ....

 function callflex(){

 alert("got it");
  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.