Call AS3 functions from javascript and vice versa

To communicate between JavaScript and Actionscript it isn’t so hard.

FLASH:

import flash.external.ExternalInterface;
ExternalInterface.addCallback("sendTextToFlash", getTextFromJavaScript);

function getTextFromJavaScript(str):void {
    trace(str);
}

JAVASCRIPT:

 var currentPage="Home";
    function setCurrentPage(newPage) {
        currentPage = newPage;
   SendDataToFlashMovie(newPage);
    }

So then on the page, this code is run when an item is clicked (javascript function sending to flash)

<a href="javascript:void(0);" onClick="setCurrentPage('Home')">Home</a>

This javascript function is to return the reference to the flash movie obj box in whatever browser:

function getFlashMovieObject(movieName){
if (window.document[movieName]){
return window.document[movieName];
}
if (navigator.appName.indexOf("Microsoft Internet")==-1){
if (document.embeds && document.embeds[movieName])
return document.embeds[movieName];
}
else{
return document.getElementById(movieName);
}
}

and then finally, to send the vars etc. to flash, crank it like so:

function SendDataToFlashMovie(newPage){
var flashMovie=getFlashMovieObject("main_flash");
flashMovie.sendTextToFlash(newPage);
}
</script>

This is all blatantly ripped from Painteddigital (just incase they take it down)
http://painteddigital.com/2008/calling-flash-as3-functions-from-javascript/

SO THEN. To communicate your actionscript to javascript, it’s really easy – just navigateToURL it:

navigateToURL(new URLRequest(String("javascript:openWindow('modules/" + listing[targetID] + "')")), "_self");

bit easier below:

It’s very easy:
import flash.external.ExternalInterface;

ExternalInterface.call(“your_javascript_function()”);
You can even get a return value:
var x:int = ExternalInterface.call(“get_x()”);
To pass an argument try:
var retval:int = ExternalInterface.call(“some_js_function()”, “the-argument”);

http://codingrecipes.com/calling-a-javascript-function-from-actionscript-3-flash

Tech Reference: ,

Leave a Reply

Your email address will not be published. Required fields are marked *

Proudly powered by WordPress | Theme: Baskerville 2 by Anders Noren.

Up ↑