<script>: RTCode attributes

RTCode.io adds two Boolean attributes to the standard <script> element: reload and pure. In the playground, a script also decides when the output reloads. A hot patch never runs a script, so an edit to an executable script reloads the output instead, and these attributes let you steer that. Everything else about the element is the <script> you know.

Try it

The counter keeps running while you edit its script. The edit hot-patches the script's text, and the new code runs only after F5. The oi:exclude attribute keeps the ticking value out of the source.

html
<output id=n oi:exclude>0</output>
<script pure>
  setInterval(() => n.value = +n.value + 1, 1000)
</script>

Attributes

This element includes the global attributes and the standard attributes of <script>. These are the ones RTCode.io adds:

reload

A Boolean attribute, on a script of any type. While any <script reload>, or any element with sync=reload, exists in the source, every edit anywhere is a full reload of the output. On its own, the Boolean reload counts only on <script>. On other elements, write sync=reload.

pure Experimental

A Boolean attribute. It takes the script out of the set of executable scripts whose changes reload the output. An edit to it hot-patches the script element's text, but the code is not run again until the next full reload, and the old code keeps running. Adding or removing pure changes the set, and reloads once.

Other RTCode.io attributes that work on scripts have their own pages: <script type=worker> and o:.

Content

The text of the script. For a data block, such as type=text/plain, application/json or text/markdown, it is data. An edit to a data block hot-patches and never reloads the output, unless a reload marker is set. A <script> inside a <template> is not inspected at all.

Usage notes

Hot patches never run scripts

The playground parses an edit with DOMParser and inserts the nodes it imports, so a newly inserted <script> does not run, and changing the text of an existing script never runs it again. That is why an edit to an executable script reloads the output: a reload is the only way to run the new code. See When the output reloads.

Which scripts reload the output

The output reloads when the set of executable scripts changes. A script is executable when it is not pure and its type is one of:

The comparison covers the src URLs and the whole outer HTML of those scripts, attributes and text included. Editing inside a script, adding, removing or reordering scripts, and adding or removing pure all reload the output. The type comparison is exact and case-sensitive, so write types in lowercase.

Reload every edit

A reload marker makes every edit a full reload, not only edits to scripts. That also refreshes exported files, which reach the output's service worker only on a full reload. A real site that does this: rtedge.net puts reload on its module script, <script type=module reload>. See Exporting files.

In deployments

Deploying does not strip reload and pure, as it does the attributes that start with i:, o:, io:, edge:, h: and worker:. They stay in the HTML as inert attributes. Outside the playground they have no effect.

Examples

Keeping an exported file fresh

reload on the data block makes every edit a full reload, so /notes.txt and the frame always show the current text. Without it, the frame keeps the file of the last load.

html
<script reload type=text/plain o:=notes.txt>Edit me and the frame follows.</script>
<iframe src=notes.txt></iframe>

Technical summary

Permitted contentThe script text, or the text of a data block
Read fromThe source in the editor
Deployed HTMLKept: reload and pure stay as inert attributes
Applies inThe playground
DOM interfaceHTMLScriptElement
Element<script>

Specifications

Not part of any specification. reload and pure are RTCode.io extensions of the standard <script> element.

Availability

FeaturePlaygroundRTEdge.netNotes
<script reload>YesNoKept in the deployed HTML as an inert attribute.
<script pure> ExperimentalYesNoWorks, but is not listed in the playground's hover help. May change. Kept in the deployed HTML as an inert attribute.

See also