Skip to content
AI Learning Ground Start learning

News ·

Inline rendering with document​.currentScript

Simon Willison links to Lean Rada's trick using document.currentScript to have an inline script element replace itself with rendered content when the page loads.

Simon Willison has linked to a post by Lean Rada describing a small browser trick built on the document.currentScript property. A tiny inline <script> element can replace itself as the page loads, so the rendered output appears exactly where the tag was written.

The example shown is 'The year is <script> document.currentScript.replaceWith( new Date().getFullYear())</script>', which replaces the script tag with the current year. By defining a shortcut function, $=(...n)=>document.currentScript.replaceWith(...n), the pattern becomes even shorter, turning it into a minimal inline template language like 'The year is <script>$(new Date().getFullYear())</script>'.

For anyone building web interfaces around AI tools, this offers a zero-dependency way to inject computed values inline without a framework or templating engine. It works in plain HTML with a one-line helper, which is handy for quick demos, generated pages, or lightweight frontends.

References

Read the full story at Simon Willison ↗Like, save or discuss on Buzz

#tooling #prompting