1

Using jquery,JS can we call a function in flex code.Below is that i have a button which calls a AS code.Can this be done if so how ?

 <script>
   function callas()
   {
    addBody();//call flex function        
   }
 </script>

 <input type="button" onclick="callas();" />

FLEX code

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">


<mx:Script>
<![CDATA[

import mx.controls.Button;
import mx.controls.Alert;
import flash.display.InteractiveObject;
import flash.display.Sprite;
import flash.media.*;
import flash.net.*;


public function addBody():void
{

  Alert.show("Got input from JS");

}
</mx:Script>


</mx:Application >

2 Answers 2

1

It should be something like this:

Javascript

function getFlashMovie(movieName) {
    document.getElementById(movieName).setAttribute("name", movieName);
    var isIE = navigator.appName.indexOf("Microsoft") != -1;
    return (isIE) ? window[movieName] : document[movieName];
}

function callas()
{
   // You need to know the ID of the object/embed tag; swfobject has an attribute for that. see http://code.google.com/p/swfobject/wiki/documentation#How_can_you_configure_your_Flash_content?
   var swfobjectID = 'myFlashObjectID'  
   //call flex function 
   getFlashMovie(swfobjectID).addBody();
}

Actionscript / flex

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">

<mx:Script>
<![CDATA[

import mx.controls.Button;
import mx.controls.Alert;
import flash.display.InteractiveObject;
import flash.display.Sprite;
import flash.media.*;
import flash.net.*;
import flash.external.ExternalInterface;

//                "javascript function", flash function
ExternalInterface.addCallback("addBody", addBody);

public function addBody():void
{
  Alert.show("Got input from JS");
}
</mx:Script>

</mx:Application >

sources:
http://kb2.adobe.com/cps/156/tn_15683.html
http://code.google.com/p/swfobject/wiki/documentation#How_can_you_configure_your_Flash_content?

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

Comments

0

You need to utilize an Actionscript class named ExternalInterface to interact with Javascript.

So, if you'd like to call a Javascript function from Flex or Flash, then you should use something like this:

ExternalInterface.call("Javscriptfunction", parameters);

Should you want to call Actionscript from Javascript, try the following:

ExternalInterface.addCallback("javascriptfunc", flexfunc);

protected function flexfunc(result:String):void{
    trace(result);
}

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.