I need to execute some JavaScript string code from flash swf file using actionscript 3. I read that it could be done by passing the JavaScript code to the parent html document , using externalinterface.call function. Then I assume it will be faster if I can declare functions in the JavaScript code in the HTML document in the first use of externalinterface.call function ; if flash code calls the JavaScript code repeatedly. So let me ask you how to do that. For details , any JavaScript to be loaded is unknown in design time and I can not prepare a JavaScript file to be loaded.
1
votes
What did you try? Just searching for "externalinterface" on Google gives you 10 tutorials on how to use it.
- laurent
@Laurent,you can search correct answers because you know correct keywords. For my case , 1000 tutorials in Google search describe just how to call javascript function in an html file. Finding my case from them is impossible without suitable keywords.
- seven_swodniw
2 Answers
2
votes
You can try this:
var js:XML = <code><![CDATA[
function() {
window.myFunc = function(arg) {
alert(arg);
}
}
]]></code>;
ExternalInterface.call(js);
and later on you can safely call:
ExternalInterface.call("myFunc", "myArgument");
Note that using an xml is only a way to comfortably write the Javascript code (with indentation and multilines) ;)
0
votes
FYI, alternately to using an XML Object you can call ExternalInterface on eval using a String containing JavaScript code.
var myJSCode:String = "window.log = function(msg) { if (console) console.log(msg) }"
ExternalInterface.call("eval", myJSCode);
And later call your function:
ExternalInterface.call("log", "Hello from ActionScript");