tags:

views:

44

answers:

2

Hi,

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

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
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
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
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
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
Check this answer too http://stackoverflow.com/questions/2307335/security-error-when-trying-to-call-actionscript-function-from-js
Faheem
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
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
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
A: 

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
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
Can anyone one send me working project calling JavaScript function with full code ?
FlexJogger