document.currentScript trick lets inline scripts replace themselves

In a link-blog post dated 17 October 2026, Simon Willison points to Lean Rada, who describes a neat trick built on the document.currentScript property. The idea is to drop a tiny inline <script> element into a page that replaces itself when the page loads.

The basic example puts the current year into running text. The script calls document.currentScript.replaceWith(new Date().getFullYear()) inside a <script> element placed right after the words "The year is". When it runs, the script element is swapped out for the year, so the reader sees only the number.

Willison then shows a shortcut. If you define a function like this: $=(...n)=>document.currentScript.replaceWith(...n), the same example shrinks to "The year is <script>$(new Date().getFullYear())</script>". Willison describes the result as a tiny template language: each inline script call computes a value and takes the place of the tag that holds it.

The post is short. It gives the idea, the two code examples and the shortcut definition, and adds no further commentary beyond calling it a neat trick.

Key facts

  • Lean Rada describes a trick that uses the document.currentScript property to place a tiny inline <script> element that replaces itself when the page loads.
  • Example: document.currentScript.replaceWith(new Date().getFullYear()) swaps the script element for the current year.
  • A shortcut function, $=(...n)=>document.currentScript.replaceWith(...n), shortens the call to <script>$(new Date().getFullYear())</script>.
  • Willison calls the result a tiny template language.
  • The post is a link-blog entry dated 17 October 2026.

Why it matters

It is a small web-development idea, not a product or a release. The appeal is how little code it needs: one property and one short helper let you put computed values directly into HTML text, so the page works like a template without a templating library. Willison presents it as a neat trick.

Who it affects

Web developers who write HTML by hand or generate simple pages and want to insert a computed value, such as the current year, at a specific spot in the text. The source does not name any other audience.

How to use it

Place an inline <script> element where the value should appear and have it call document.currentScript.replaceWith(...) with the value. For the year, that is document.currentScript.replaceWith(new Date().getFullYear()). To shorten it, define $=(...n)=>document.currentScript.replaceWith(...n) once, then write <script>$(new Date().getFullYear())</script> wherever you need it.

How solid is it

This is a short link-blog post by Simon Willison that points to Lean Rada's description of the trick and shows two working-looking snippets. It is a pointer, not a test or a benchmark. No link or title of Lean Rada's original article is given in the extracted text, and the source does not say who Lean Rada is beyond being the person who describes the trick.

Risks and caveats

The post mentions no browser support, performance, security or CSP considerations, and gives no caveats about when the script runs beyond saying it replaces itself when the page loads. Anyone adopting it should check those points themselves.