Skip to Content
defold-typescript
Get startedGuidesAPILibraries
Defold

html5

HTML5 platform specific functions.

The following functions are only available on HTML5 builds, the html5.* Lua namespace will not be available on other platforms.

Functions

html5.run(code: string): string

Executes the supplied string as JavaScript inside the browser. A call to this function is blocking, the result is returned as-is, as a string. (Internally this will execute the string using the eval() JavaScript function.)

const res = html5.run("10 + 20"); // returns the string "30"
print(res);
const res_num = tonumber(res); // convert to number
if (res_num !== undefined) {
  print(res_num - 20); // prints 10
}
local res = html5.run("10 + 20") -- returns the string "30"
print(res)
local res_num = tonumber(res) -- convert to number
print(res_num - 20) -- prints 10

Parameters

  • code: string — Javascript code to run

Returns

  • result: string — result as string

html5.set_interaction_listener(callback: ((self: unknown) => void) | undefined): void

Set a JavaScript interaction listener callaback from lua that will be invoked when a user interacts with the web page by clicking, touching or typing. The callback can then call DOM restricted actions like requesting a pointer lock, or start playing sounds the first time the callback is invoked.

function on_interaction(self: unknown) {
  print("on_interaction called");
  html5.set_interaction_listener(undefined);
}

export default defineScript({
  init() {
    html5.set_interaction_listener(on_interaction);
  },
});
local function on_interaction(self)
    print("on_interaction called")
    html5.set_interaction_listener(nil)
end

function init(self)
    html5.set_interaction_listener(on_interaction)
end

Parameters

  • callback: ((self: unknown) => void) | undefined — The interaction callback. Pass an empty function or nil if you no longer wish to receive callbacks. self script_instance The calling script instance

On this page