Accessing an ActionScript function via Javascript
Asked Answered
Z

2

6

I'm trying to call a function in an action script using the ExternalInterface.addCallback API, but I can't seem to get it to work. Here's what I have:

ActionScript:

//MyClass.as  
package {

    import flash.display.Sprite;
    import flash.external.ExternalInterface;

    public class MyClass extends Sprite
    {
        public function MyClass()
        {
            ExternalInterface.addCallback('getStringJS', getStringAS);
        }

        public function getStringAS():String
        {
            return "Hello World!";
        }
    }
}

NOTE: I'm compiling this into an swf using the flex mxmlc compiler if that matters.

HTML/Javascript:

<!doctype html>
<html>
    <head>
        <title>User Identification</title>
    <head>
    <body>
        <object id="MyClass" name="MyClass" type="application/x-shockwave-flash" data="MyClass.swf" width="1" height="1">
            <param name="movie" value="MyClass.swf">
            <embed src="MyClass.swf" width="1" height="1">
        </object>
        <script type="text/javascript">
            var flash = document.getElementById("MyClass");
            var str = flash.getStringJS();
            alert(str);
        </script>
    </body>
</html>

The error I'm getting is:

Uncaught TypeError: Object #<HTMLObjectElement> has no method 'getStringJS'

I also tried adding in a timeout in case the swf file wasn't loading, but I didn't have any success with that method either.

Any thoughts?

Cheers,
Mike

Zorine answered 1/6, 2011 at 22:39 Comment(0)
Z
5

I figured it out. The key way to signal the javascipt through ExternalInterface.call so we know for sure that the swf is loaded. The most "Universal" way to do this is as follows:

MyClass.as

//MyClass.as  
package {

    import flash.display.Sprite;
    import flash.external.ExternalInterface;

    public class MyClass extends Sprite
    {
        public function MyClass()
        {
            ExternalInterface.addCallback('getStringJS', getStringAS);
            if  (ExternalInterface.available) {
                ExternalInterface.call("isConnectedFlex");
            }
        }

        public function getStringAS():String
        {
            return "Hello World!";
        }
    }
}

index.html

<!doctype html>
<html>
    <head>
        <title>User Identification</title>
    <head>
    <body>
        <object id="MyClass" name="MyClass" type="application/x-shockwave-flash" data="MyClass.swf" width="1" height="1">
            <param name="movie" value="MyClass.swf">
            <embed src="MyClass.swf" width="1" height="1">
        </object>
        <script type="text/javascript">

            var flash = document.getElementById("MyClass");

            function isConnectedFlex() {
                var str = flash.getStringJS();
                alert(str);
            }


        </script>
    </body>
</html>
Zorine answered 1/6, 2011 at 23:24 Comment(1)
Nice example. Works still in 2017. For others, some other additional information can be found here as well: help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/…Mixture
B
2

I think the issue is a matter of the flash not being loaded. I tried your code using the window.onload event and it worked for me:

The flash is the same...

HTML/JS :

<!doctype html>
<html>
    <head>
        <title>User Identification</title>
    <head>
    <body>

        <object id="MyClass" name="MyClass" type="application/x-shockwave-flash" data="MyClass.swf" width="1" height="1">
            <param name="movie" value="MyClass.swf">
            <embed src="MyClass.swf" width="1" height="1">
        </object>

        <script>

            window.onload = function() {

                var flash = document.getElementById("MyClass");
                var test = flash.getStringJS("test");
                alert(test);   //pops up with "Hello World!" on Firefox

            };

        </script>
    </body>
</html>  

Does that help?

Besetting answered 1/6, 2011 at 23:8 Comment(2)
Yeah, it made me realize I really need to learn to check my work in other browsers haha. It works on Firefox and IE, but not chrome. Either way I still need a more universally acceptable method. Any thoughts?Zorine
I see. You might want to check out this link: problem-accessing-externalinterface-exposed-method-in-google-chromeBesetting

© 2022 - 2024 — McMap. All rights reserved.