class_javascript.rst 7.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. :github_url: hide
  2. .. Generated automatically by doc/tools/make_rst.py in Godot's source tree.
  3. .. DO NOT EDIT THIS FILE, but the JavaScript.xml source instead.
  4. .. The source is found in doc/classes or modules/<name>/doc_classes.
  5. .. _class_JavaScript:
  6. JavaScript
  7. ==========
  8. **Inherits:** :ref:`Object<class_Object>`
  9. Singleton that connects the engine with the browser's JavaScript context in HTML5 export.
  10. Description
  11. -----------
  12. 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.
  13. **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.
  14. Tutorials
  15. ---------
  16. - `Exporting for the Web: Calling JavaScript from script <../tutorials/export/exporting_for_web.html#calling-javascript-from-script>`__
  17. Methods
  18. -------
  19. +-------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  20. | :ref:`JavaScriptObject<class_JavaScriptObject>` | :ref:`create_callback<class_JavaScript_method_create_callback>` **(** :ref:`Callable<class_Callable>` callable **)** |
  21. +-------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  22. | :ref:`Variant<class_Variant>` | :ref:`create_object<class_JavaScript_method_create_object>` **(** :ref:`String<class_String>` object, ... **)** |vararg| |
  23. +-------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  24. | void | :ref:`download_buffer<class_JavaScript_method_download_buffer>` **(** :ref:`PackedByteArray<class_PackedByteArray>` buffer, :ref:`String<class_String>` name, :ref:`String<class_String>` mime="application/octet-stream" **)** |
  25. +-------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  26. | :ref:`Variant<class_Variant>` | :ref:`eval<class_JavaScript_method_eval>` **(** :ref:`String<class_String>` code, :ref:`bool<class_bool>` use_global_execution_context=false **)** |
  27. +-------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  28. | :ref:`JavaScriptObject<class_JavaScriptObject>` | :ref:`get_interface<class_JavaScript_method_get_interface>` **(** :ref:`String<class_String>` interface **)** |
  29. +-------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  30. Method Descriptions
  31. -------------------
  32. .. _class_JavaScript_method_create_callback:
  33. - :ref:`JavaScriptObject<class_JavaScriptObject>` **create_callback** **(** :ref:`Callable<class_Callable>` callable **)**
  34. Creates a reference to a :ref:`Callable<class_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<class_JavaScriptObject>` for usage.
  35. ----
  36. .. _class_JavaScript_method_create_object:
  37. - :ref:`Variant<class_Variant>` **create_object** **(** :ref:`String<class_String>` object, ... **)** |vararg|
  38. Creates a new JavaScript object using the ``new`` constructor. The ``object`` must a valid property of the JavaScript ``window``. See :ref:`JavaScriptObject<class_JavaScriptObject>` for usage.
  39. ----
  40. .. _class_JavaScript_method_download_buffer:
  41. - void **download_buffer** **(** :ref:`PackedByteArray<class_PackedByteArray>` buffer, :ref:`String<class_String>` name, :ref:`String<class_String>` mime="application/octet-stream" **)**
  42. Prompts the user to download a file containing the specified ``buffer``. The file will have the given ``name`` and ``mime`` type.
  43. **Note:** The browser may override the `MIME type <https://en.wikipedia.org/wiki/Media_type>`__ provided based on the file ``name``'s extension.
  44. **Note:** Browsers might block the download if :ref:`download_buffer<class_JavaScript_method_download_buffer>` is not being called from a user interaction (e.g. button click).
  45. **Note:** Browsers might ask the user for permission or block the download if multiple download requests are made in a quick succession.
  46. ----
  47. .. _class_JavaScript_method_eval:
  48. - :ref:`Variant<class_Variant>` **eval** **(** :ref:`String<class_String>` code, :ref:`bool<class_bool>` use_global_execution_context=false **)**
  49. Execute the string ``code`` as JavaScript code within the browser window. This is a call to the actual global JavaScript function ``eval()``.
  50. 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.
  51. ----
  52. .. _class_JavaScript_method_get_interface:
  53. - :ref:`JavaScriptObject<class_JavaScriptObject>` **get_interface** **(** :ref:`String<class_String>` interface **)**
  54. 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<class_Array>` argument, which will contain the JavaScript ``arguments``. See :ref:`JavaScriptObject<class_JavaScriptObject>` for usage.
  55. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  56. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  57. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
  58. .. |constructor| replace:: :abbr:`constructor (This method is used to construct a type.)`
  59. .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`
  60. .. |operator| replace:: :abbr:`operator (This method describes a valid operator to use with this type as left-hand operand.)`