| Safe Haskell | Safe-Inferred |
|---|---|
| Language | Haskell2010 |
Hypermedia.Datastar.ExecuteScript
Description
Sometimes you need a one-shot browser-side effect that doesn't fit neatly
into DOM patching or signal updates — redirecting to another page, focusing
an input, triggering a download, or calling a browser API.
executeScript lets the server push arbitrary JavaScript to the browser.
Under the hood, Datastar appends a <script> tag to <body>. By default
the script tag removes itself from the DOM after executing (see esAutoRemove).
Note: per the Datastar protocol, script execution uses the
datastar-patch-elements event type — there is no separate event type for
scripts.
sendExecuteScript gen (executeScript "window.location = \"/dashboard\"")
To add attributes to the generated <script> tag:
sendExecuteScript gen
(executeScript "import(\"/modules/chart.js\").then(m => m.render())")
{ esAttributes = ["type=\"module\""] }
Synopsis
- data ExecuteScript = ExecuteScript {
- esScript :: Text
- esAutoRemove :: Bool
- esAttributes :: [Text]
- esEventId :: Maybe Text
- esRetryDuration :: Int
- executeScript :: Text -> ExecuteScript
- toDatastarEvent :: ExecuteScript -> DatastarEvent
- buildScriptLines :: ExecuteScript -> [Text]
Documentation
data ExecuteScript Source #
Configuration for executing a script in the browser.
Construct values with executeScript, then customise with record updates.
Constructors
| ExecuteScript | |
Fields
| |
Instances
| Show ExecuteScript Source # | |
Defined in Hypermedia.Datastar.ExecuteScript Methods showsPrec :: Int -> ExecuteScript -> ShowS # show :: ExecuteScript -> String # showList :: [ExecuteScript] -> ShowS # | |
| Eq ExecuteScript Source # | |
Defined in Hypermedia.Datastar.ExecuteScript Methods (==) :: ExecuteScript -> ExecuteScript -> Bool # (/=) :: ExecuteScript -> ExecuteScript -> Bool # | |
executeScript :: Text -> ExecuteScript Source #
Build an ExecuteScript event with sensible defaults.
The argument is the JavaScript source code to run in the browser.
executeScript "document.getElementById(\"name\").focus()"
buildScriptLines :: ExecuteScript -> [Text] Source #