Flash – Javascript communication

Important: IE uses the id to grab the flash movie, all other browsers use the name.

SOURCE: http://www.actionscript.org/resources/articles/638/1/Basics-of-using-the-ExternalInterface/Page1.html

Javascript:

function onChange( str:String ){

// handle call from outside flash

}

// tells it the property in JS, the scope, and the return function to call

ExternalInterface.addCallback( “onChange”, this, onChange );

onMouseDown = function(){

ExternalInterface.call(“general_JS_call”, “String to return to Flash:”);

// calling the function name first, arguments of the function following

}

HTML:

<html>

<head>

<title>Test</title>

</head>

<body onload=’getID(“catalogue”);’>

<script type=”text/javascript”>

var me;

function getID(swfID) {

if (navigator.appName.indexOf(“Microsoft”) != -1) {

me = window[swfID];

} else {

me = document[swfID];

}

}

function js_to_as(str) {

me.onChange(str); //function name equal to the callback defined in actionscript

}

function general_JS_call(str) {

//react to call from actionscript

}

</script>

<object classid=”clsid:D27CDB6E-AE6D-11cf-96B8-444553540000″ codebase=”http://macromedia.com/cabs/swflash.cab#version=4,0,0,0″

id=”swf_name” width=”1398″ height=”636″>

<param name=”movie” value=”swf_name.swf” />

<param name=”FlashVars” value=”” />

<param name=”quality” value=”medium” />

<param name=”bgcolor” value=”#99CC33″ />

<embed src=”swf_name.swf” flashvars=”” bgcolor=”#99CC33″ width=”1398″

height=”636″ type=”application/x-shockwave-flash” name=’swf_name’>

</embed>

</object>

<input type=”button” value=”Pass to AS” onclick=”js_to_as(‘Test value to send to AS’);”>

</body>

</html>


Skip to toolbar