Access ActionScript Function Through Javascript

I am trying to call a function in a script action using the ExternalInterface.addCallback API, but I cannot get it to work. Here is 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 compile this in swf using the mxmlc flex 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 am getting is:

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

I also tried to add a timeout in case the swf file was not loaded, but I also did not succeed in this method.

Any thoughts?

Cheers
Mike

+6
source share
2 answers

I get it. The main way to signal javascipt is via ExternalInterface.call , so we know for sure that swf is loaded. The most “universal” way to do this:

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> 
+5
source

I think the problem is that the flash does not load. I tried my code using the window.onload event and this 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 it help?

+2
source

Source: https://habr.com/ru/post/889588/


All Articles