For HTML5 games, how might I go about communicating with javascript that’s preexisting on the page. For example, maybe I have a function called submitData(data)
. How would I call that within the game?
There’s a couple ways, one would be to use the ExternalInterface
class. We have an example that shows how this could work, both with JavaScript and with Flash Player:
You can create a copy using the following command:
openfl create BrowserExternalInterface
Another way is to use untyped __js__
to add literal JavaScript into your code, so long as you are compiling to the HTML5 target:
#if html5
untyped __js__ ("submitData") (data);
#end
There are also ways to use Haxe extern classes