# Tyxml - Inserting script tags?

**URL:** <https://discuss.ocaml.org/t/tyxml-inserting-script-tags/13939>\
**Category:** Learning\
**Tags:** dream\
**Created:** [January 26, 2024, 9:32pm UTC](https://discuss.ocaml.org/t/tyxml-inserting-script-tags/13939 "2024-01-26T21:32:32Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![MarcCoquand](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/marccoquand/32/4901_2.png) [@MarcCoquand](https://discuss.ocaml.org/u/MarcCoquand)\
**Post date:** [January 26, 2024, 9:32pm UTC](https://discuss.ocaml.org/t/tyxml-inserting-script-tags/13939/1 "2024-01-26T21:32:32Z")

</div>

Heya!

I’m trying to understand the syntax for inserting javascript into tyxml template engine? I want to insert a script like this

```auto
<script>
       document.body.addEventListener('htmx:configRequest', (event) => { \
        event.detail.headers['X-CSRFToken'] = 'MYTOKEN'; });
</script>

```

Can’t for the life of me figure out how it’s supposed to be done.

---

<div class="post-metadata">

**Author:** ![yawaramin](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/yawaramin/32/3384_2.png) [@yawaramin](https://discuss.ocaml.org/u/yawaramin)\
**Post date:** [January 26, 2024, 9:37pm UTC](https://discuss.ocaml.org/t/tyxml-inserting-script-tags/13939/2 "2024-01-26T21:37:31Z")

</div>

Does `script (txt {|...|})` not work?

Eg [tyxml/examples/basic\_website/site\_html.ml at 06b5c2afb4318eaea813151b2a0047212a0c277f · ocsigen/tyxml · GitHub](https://github.com/ocsigen/tyxml/blob/06b5c2afb4318eaea813151b2a0047212a0c277f/examples/basic_website/site_html.ml#L46)

---

<div class="post-metadata">

**Author:** ![MarcCoquand](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/marccoquand/32/4901_2.png) [@MarcCoquand](https://discuss.ocaml.org/u/MarcCoquand)\
**Post date:** [January 26, 2024, 9:40pm UTC](https://discuss.ocaml.org/t/tyxml-inserting-script-tags/13939/3 "2024-01-26T21:40:28Z")

</div>

Not really, testing

```auto

let my_function =
  let open Tyxml.Html in
  script
    []
    (txt
       "document.body.addEventListener('htmx:configRequest', (event) => {event.detail.headers['X-CSRFToken'] = 'tokentobeaddedasarg';")

```

Gives the incomprehensible error

```auto
This expression has type 'a list_wrap but an expression was expected of type
  ([< Html_types.script_content_fun] as 'b) elt = 'b elt

```

---

<div class="post-metadata">

**Author:** ![yawaramin](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/yawaramin/32/3384_2.png) [@yawaramin](https://discuss.ocaml.org/u/yawaramin)\
**Post date:** [January 26, 2024, 9:42pm UTC](https://discuss.ocaml.org/t/tyxml-inserting-script-tags/13939/4 "2024-01-26T21:42:55Z")

</div>

Tyxml attributes are given with an optional named argument `~a:...`. If you omit the name it thinks that you’re trying to pass in an empty list of children elements, hence the type error. Just completely omit the attribute list if it’s empty.

---

<div class="post-metadata">

**Author:** ![MarcCoquand](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/marccoquand/32/4901_2.png) [@MarcCoquand](https://discuss.ocaml.org/u/MarcCoquand)\
**Post date:** [January 26, 2024, 9:45pm UTC](https://discuss.ocaml.org/t/tyxml-inserting-script-tags/13939/5 "2024-01-26T21:45:28Z")

</div>

Great, now it’s compiling, thanks!

However it escapes the `=>` and turns it into `=&gt`

---

<div class="post-metadata">

**Author:** ![yawaramin](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/yawaramin/32/3384_2.png) [@yawaramin](https://discuss.ocaml.org/u/yawaramin)\
**Post date:** [January 26, 2024, 9:48pm UTC](https://discuss.ocaml.org/t/tyxml-inserting-script-tags/13939/6 "2024-01-26T21:48:34Z")

</div>

Use `Unsafe.data` instead of `txt`: [tyxml/lib/html\_sigs.mli at 06b5c2afb4318eaea813151b2a0047212a0c277f · ocsigen/tyxml · GitHub](https://github.com/ocsigen/tyxml/blob/06b5c2afb4318eaea813151b2a0047212a0c277f/lib/html_sigs.mli#L1136)

---

<div class="post-metadata">

**Author:** ![MarcCoquand](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/marccoquand/32/4901_2.png) [@MarcCoquand](https://discuss.ocaml.org/u/MarcCoquand)\
**Post date:** [January 26, 2024, 9:54pm UTC](https://discuss.ocaml.org/t/tyxml-inserting-script-tags/13939/7 "2024-01-26T21:54:50Z")

</div>

Great that worked, thank you so much!
