跳到正文
原文
Simon Willison·· 2 天前AI 评分5

Lean Rada 的 document.currentScript 内联渲染技巧

Inline rendering with document​.currentScript

AI 导读

Lean Rada 提出利用 document.currentScript 属性实现内联渲染的技巧,通过定义快捷函数 $=(...n) => document.currentScript.replaceWith(...n),实现微型模板语言功能。

原文

17th October 2026 - Link Blog

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>

来源:Simon Willison · simonwillison.net