SyncAI.news, a Varaisys broadcasting
Inline rendering with document​.currentScript
SW

Simon Willison's Weblog

· 1 min read

AnalysisSimon Willison's Weblog

Inline rendering with document​.currentScript

Inline rendering with document​.currentScript

Lean Rada describes a neat trick that takes advantage of the document.currentScript property. You can use that to drop in a tiny inline <script> element that replaces itself when the page loads:
The year is <script>
document.currentScript.replaceWith(
  new Date().getFullYear())</script>

If you define a shortcut function like this:

$=(...n)=>document.currentScript.replaceWith(...n)

You get a tiny template language:

The year is <script>$(new Date().getFullYear())</script>

Original source

This story was published by Simon Willison's Weblog. SyncAI.news shows a preview; the complete article is on the publisher's site.

Read the full story on simonwillison.net

Similar News