I'm using the Racket webserver.
I've discovered I have to use javascript on the client side.
How do I generate a web page that contains javascript?
I'm not asking how to write javascript. I've done that before. I'm asking how to set it up so that it is properly packaged within the response to a request to that it arrives in the client browser as a properly declared script that can be executed in the browser.
Example: the HTML in a response gets conveniently coded as an xexpr, and the Racket webserver sends it to the client as proper xhtml. How do I do this for some embedded scripts? Even if I hand-code the javascript source code as a string, how do I embed it into the response?
How are you generating responses generally?
With xexprs, you can embed JS in attributes by just making a string, such as
(input ([class "player-initiative-input"]
[type "text"]
[inputmode "numeric"]
[value ,(~a (player-initiative p))]
[oninput
"for (element of
document.querySelectorAll(\".player-initiative-input\")) { if (element
!== this) { element.disabled = true; } }"]))
I think you can also embed cdata
structs via make-cdata
in an
xexpr that is translated by response/xexpr
and friends as "just the
string I meant, no translations."
But I find it convenient to serve the JS as a static file and then
include the right metadata via a script tag. Search for "static" in
https://github.com/benknoble/frosthaven-manager/blob/main/server.rkt.