:github_url: hide .. Generated automatically by doc/tools/make_rst.py in Godot's source tree. .. DO NOT EDIT THIS FILE, but the JavaScript.xml source instead. .. The source is found in doc/classes or modules//doc_classes. .. _class_JavaScript: JavaScript ========== **Inherits:** :ref:`Object` Singleton that connects the engine with the browser's JavaScript context in HTML5 export. Description ----------- The JavaScript singleton is implemented only in the HTML5 export. It's used to access the browser's JavaScript context. This allows interaction with embedding pages or calling third-party JavaScript APIs. **Note:** This singleton can be disabled at build-time to improve security. By default, the JavaScript singleton is enabled. Official export templates also have the JavaScript singleton enabled. See :doc:`Compiling for the Web <../development/compiling/compiling_for_web>` in the documentation for more information. Tutorials --------- - `Exporting for the Web: Calling JavaScript from script <../tutorials/export/exporting_for_web.html#calling-javascript-from-script>`__ Methods ------- +-------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`JavaScriptObject` | :ref:`create_callback` **(** :ref:`Callable` callable **)** | +-------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`Variant` | :ref:`create_object` **(** :ref:`String` object, ... **)** |vararg| | +-------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | void | :ref:`download_buffer` **(** :ref:`PackedByteArray` buffer, :ref:`String` name, :ref:`String` mime="application/octet-stream" **)** | +-------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`Variant` | :ref:`eval` **(** :ref:`String` code, :ref:`bool` use_global_execution_context=false **)** | +-------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`JavaScriptObject` | :ref:`get_interface` **(** :ref:`String` interface **)** | +-------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ Method Descriptions ------------------- .. _class_JavaScript_method_create_callback: - :ref:`JavaScriptObject` **create_callback** **(** :ref:`Callable` callable **)** Creates a reference to a :ref:`Callable` that can be used as a callback by JavaScript. The reference must be kept until the callback happens, or it won't be called at all. See :ref:`JavaScriptObject` for usage. ---- .. _class_JavaScript_method_create_object: - :ref:`Variant` **create_object** **(** :ref:`String` object, ... **)** |vararg| Creates a new JavaScript object using the ``new`` constructor. The ``object`` must a valid property of the JavaScript ``window``. See :ref:`JavaScriptObject` for usage. ---- .. _class_JavaScript_method_download_buffer: - void **download_buffer** **(** :ref:`PackedByteArray` buffer, :ref:`String` name, :ref:`String` mime="application/octet-stream" **)** Prompts the user to download a file containing the specified ``buffer``. The file will have the given ``name`` and ``mime`` type. **Note:** The browser may override the `MIME type `__ provided based on the file ``name``'s extension. **Note:** Browsers might block the download if :ref:`download_buffer` is not being called from a user interaction (e.g. button click). **Note:** Browsers might ask the user for permission or block the download if multiple download requests are made in a quick succession. ---- .. _class_JavaScript_method_eval: - :ref:`Variant` **eval** **(** :ref:`String` code, :ref:`bool` use_global_execution_context=false **)** Execute the string ``code`` as JavaScript code within the browser window. This is a call to the actual global JavaScript function ``eval()``. If ``use_global_execution_context`` is ``true``, the code will be evaluated in the global execution context. Otherwise, it is evaluated in the execution context of a function within the engine's runtime environment. ---- .. _class_JavaScript_method_get_interface: - :ref:`JavaScriptObject` **get_interface** **(** :ref:`String` interface **)** Returns an interface to a JavaScript object that can be used by scripts. The ``interface`` must be a valid property of the JavaScript ``window``. The callback must accept a single :ref:`Array` argument, which will contain the JavaScript ``arguments``. See :ref:`JavaScriptObject` for usage. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)` .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)` .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)` .. |constructor| replace:: :abbr:`constructor (This method is used to construct a type.)` .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)` .. |operator| replace:: :abbr:`operator (This method describes a valid operator to use with this type as left-hand operand.)`