[SOLVED] External call, like ExternalInterface

I have to put my JS/HTML5 application inside an html page, and my client wants to add external button to directly access specific functions inside the JS, as if I clicked/touched specific buttons inside the application.
In AS3 it worked with ExternalInterface, how do I replicate it now? I already have a fake_MouseUp function in my application, so I can simulate the interaction with buttons even when there are no real mouseUp events, I just need to know how I can call it from outside the app JS.


From html to openfl :


in openfl

     #if html5
     @:keep @:expose("external.send")
     public static function send(data:String):Void
	     trace('parameters : ', data);

@:keep will avoid dce to remove the function

@:expose will expose the method outside openfl app

1 Like

Great! Thanks!

can I remove the “static”? I have to call non-static functions inside this one.

…and can I expose single methods directly or I need to expose the whole class?

I have now created a specific class with static methods:

#if html5
@:keep @:expose("external.triggerButton")
class ExposedButtons {

public static var aButton: Button;
public static var bButton: Button;
public static var cButton: Button;
public static function triggerButton(buttonId: String): Void {
	var buttonToTrigger: Button =	switch(buttonId) {

EDIT: by exposing the method it doesn’t work, but by exposing the class it works

@:expose("external.ExposedButtons ")

-> external.ExposedButtons.triggerButton(“XXX”);

The function is shown as static here because, when the function is called by the button-pusher, there is no “object.”   If you need to refer to the method of some object, you need to first call a static function which can, in turn, find the appropriate object and call it.

I am having problems in IE and Edge, as the strict mode does not allow to use triggerButton function.
If I use @:expose("external.ExposedButtons ") I have the problem.
If I use @:expose("external.triggerButton ") directly it doesn’t throw the strict mode error, but it says the function does not exist.

public static function MyFunction()

This method works fine for me both in IE (ie9) and Edge. When I call ExposedFunction() in js I can see “success!” logged in the console panel.

It works! Thank you @hcwdikk !
It looks like “external.” creates some problem… maybe strict mode does not allow to use nested paths, but just direct references?

What I was doing wrong was also the position of the @:expose(“ExposedFunction”) line: it needs to be before the function declaration… it sounds obvious, right? Well, it was not for me, I put it before class name, my fault.