Using JavaScript ================ .. note:: JavaScript support is new in version 4.1. Embedded Python support is new in version 4.19. Make sure you are using the latest version of TkinterWeb. Overview -------- **Scripting support makes it easy to embed JavaScript or Python code in your document.** JavaScript is fully supported through Mozilla's SpiderMonkey engine, but not all DOM commands are supported. See the :doc:`api/htmldocument` for an exhaustive list of supported DOM commands. Setup ------ To enable JavaScript support in TkinterWeb, first install `PythonMonkey `_ using pip: .. code-block:: console $ pip install pythonmonkey Skip this step if you are embedding Python code in your document. Or when installing TkinterWeb, use: .. code-block:: console $ pip install tkinterweb[javascript] Then add ``yourhtmlframe.configure(javascript_enabled=True)`` to your script or add the parameter ``javascript_enabled=True`` when creating your :class:`~tkinterweb.HtmlFrame`, :class:`~tkinterweb.HtmlLabel`, or :class:`~tkinterweb.HtmlText` widget. .. note:: If using Windows, ensure you are using an up-to-date Python version. In some Python versions prior to version 3.13, Python will crash when loading PythonMonkey. **Only enable JavaScript in documents with code you know and trust.** How-to ------ To change the color and text of a ``

`` element when clicked, you could use the following: .. code-block:: python yourhtmlframe = tkinterweb.HtmlFrame(root, messages_enabled=True, javascript_enabled=True) yourhtmlframe.load_html("""

Hello, world!

""") Add the ``defer`` attribute to the relevant ``

Hello, world!

") Using your own interpreter -------------------------- Alternatively, you can register your own callback for ``

Test

") You can also use the :attr:`on_element_script` parameter to handle event scripts (i.e. handle an element's ``onclick`` attribute). The element's corresponding Tkhtml node, relevant event, and code to execute will be passed as parameters. If needed you can always then create an :class:`~tkinterweb.dom.HTMLElement` instance from a Tkhtml node: .. code-block:: python from tkinterweb.dom import HTMLElement ... yourhtmlelement = HTMLElement(yourhtmlframe.document, yourtkhtmlnode) ------------------- It is also possible to interact with the document through Python instead. See :doc:`dom`. Please report bugs or request new features on the `issues page `_.