useScript API reference
useScript
function is designed to help developers inline scripts into a
web page with minimal payload. It receives a function and its arguments as
parameters and returns the stringified, minified version of the function. This
is particularly useful for inlining event handlers and other scripts directly
into HTML, optimizing performance by reducing the amount of JavaScript sent over
the network. It integrates seamlessly with the hx-on:
handlers of HTMX.
Function
...any[]
dangerouslySetInnerHTML
useScript
is used to inline a simple script that logs a
message when the window loads.
hx-on
AttributeuseScript
is used to create a minified event handler for an
hx-on:click
attribute that changes the text of a button when clicked.
useScript
does not rely on external
variables or closures that won’t be available when the script is executed
inline.hx-on:
handlers, make sure the minified function does not
exceed any attribute length limits that might be imposed by browsers or HTML
specifications.