Actionscript 3: Pass multiple parameters defined in external file to ExternalInterface.call()


There was a situation where the javascript function name and the number of parameters to it was defined in an external JSON file. Since the number of parameters was dynamic, we needed a way to have ExeternalInterface call with the correct number.

The solution is simple.

in the external file, pass in a string of the function name and all the parameters:

"myFunctionName,param1,param2,param3"

and in your actionscript code

	var myString:String = "myFunctionName,param1,param2,param3";				
	var paramArray:Array = myString.split(",");
	ExternalInterface.call.apply(null, paramArray);

The secret is the apply() method. All functions in actionscript is a type of object with couple of built in methods, and the apply() method allows you to pass in an array.

Test with a simple alert in your html wrapper

<script>
  function myFunctionName(value1, value2, value3) {
    alert("myFuncName fired : " + value1 + value2 + value3);
  }
</script>

Another way is to pass an object with the paramters you need attached to it and cycle thru that in javascript.

Author: mark

Creative technologist

Leave a Reply

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