Question

I'm attempting to get a basic example of an ExternalInterface call working. The aim: send a string to a flash object and have it return it, which I can then console.log.

I keep getting Uncaught TypeError: undefined is not a function when trying to call the method on my flash object.

I am using allowScriptAccess in both the object and embed tags, and I'm checking there aren't any race conditions with both a setTimeout() and using $(window).on("load") instead of $(document).ready(); - yet I still get the above error.

I have placed the following actionscript in the first keyframe of my flash animation:

import flash.external.*;
import flash.system.*;

// Params: First the function to call in JS, second the function run within AS
ExternalInterface.addCallback("test", sayHerro);

function sayHerro():String {
    return "HERRO OMFG";
}

So I have defined a function, sayHerro(), that takes no parameters and returns a String. This should be executed when my JavaScript calls test on the swf object.

Here is the HTML holding the element in the body:

<object height="640" id="myFlashMovie"> // note the id is myFlashMovie
    <param value="inbanner-mpu.swf" name="movie" />
    <param value="true" name="swLiveConnect" /> // Apparently I need this...
    <param value="always" name="allowScriptAccess" /> // and also this...

    // Note the src is the same as above (the animation does display), the name is the same, and this also has the allowscriptaccess tag
    <embed height="640" src="inbanner-mpu.swf" allowscriptaccess="always" type="application/x-shockwave-flash" name="myFlashMovie"></embed> 
</object>

And finally here is my JavaScript which waits half a second before executing and attempting to call test() on the swf object.

<script type="text/javascript">
    $(window).on("load", function() {
        setTimeout(function() {
            var swf = document.getElementById("myFlashMovie");
            console.log(swf);
            swf.test();
        }, 500);
    });
</script>

Why am I getting the TypeError? I must be doing something really daft but I just can't figure out where my issue is.

In case of relevance:

  • Browser: Chrome (latest stable)
  • Flash: CS6
Was it helpful?

Solution

Bah, this was a really silly thing - but it's pretty important so I'll leave this here for future viewers. I knew I was doing something daft.

ActionScript 2 and ActionScript 3 have different parameter requirements for ExternalInterface::addCallback() - this can really mess things up for you!

ActionScript 2

Documentation

public static addCallback(methodName: String, instance: Object, method: function)

methodName - a string that the JavaScript will call
instance - the object to which this resolves in the method. I set it to null.
method - the method you want called in your actionscript

You'll note that this method returns a boolean.

ActionScript 3

Documentation

public static addCallback(functionName: String, closure: Function)

methodName - a string that the JavaScript will call
closure - the method you want called in your actionscript

You'll note that this method returns void.


I did spend half a day trying to figure this out, so hopefully the above is useful. Check what version of actionscript you are working with first. Then use the above to figure out whether you need two parameters or three.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top