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>
MPelletier
  • 16,256
  • 15
  • 86
  • 137
FlexJogger
  • 31
  • 2
  • 10

4 Answers4

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.

Faheem
  • 3,429
  • 19
  • 26
  • 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. – FlexJogger Oct 19 '10 at 22:47
  • 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. – Faheem Oct 19 '10 at 23:17
  • 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() – FlexJogger Oct 20 '10 at 17:49
  • Have you tried addCallBack method? Also check - throws - security error 2 solution here http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/external/ExternalInterface.html#addCallback() – Faheem Oct 20 '10 at 18:47
  • Check this answer too http://stackoverflow.com/questions/2307335/security-error-when-trying-to-call-actionscript-function-from-js – Faheem Oct 20 '10 at 18:51
  • I tried same release on my mac - non dev env - and got same 2060 error. Added that folder to always allow in Global Security settings and code started working again. Answer that I have posted above explains the logic. – Faheem Oct 20 '10 at 19:24
  • Finally I got the solution.Setting to run application It’s an one time activity on each system. As of now it’s a work around. Step 1: Run following link: http://www.macromedia.com/support/documentation/en/flashplayer/help/settings_manager04.html Step 2: Click on Edit location >> Click on Add Location. Step 3: Click on “Browse for folder” and select the drive (Example C:\) from where you are running the application. Step 4: Press “Confirm” button. It will show you your selected location. – FlexJogger Oct 20 '10 at 21:34
  • And that is only because of file:/// protocol, which should not happen to AIR or Flex applications - You won't need this for deploying on client computers :) – Faheem Oct 20 '10 at 22:07
  • I can't help thinking this over and over again. Stackoverflow is amazing and you guys are rock! – Sid Jun 01 '11 at 06:25
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"...

Ouallada
  • 11
  • 3
  • 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 ? – FlexJogger Oct 19 '10 at 22:16
  • Can anyone one send me working project calling JavaScript function with full code ? – FlexJogger Oct 19 '10 at 22:19
  • @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. – Shankar Narayana Damodaran Apr 24 '13 at 09:29
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 :(

Bleeding Fingers
  • 6,993
  • 7
  • 46
  • 74
0

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

     protected function bt1_clickHandler(event:MouseEvent):void
        {

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

.js ....

 function callflex(){

 alert("got it");
  }
Deepika C P
  • 1,263
  • 2
  • 13
  • 23