Inline rendering with document​.currentScript

林知秋AI 前沿📡 Simon Willison's Weblog2026-10-18339 阅读💛 71 收藏

17th October 2026 - Link Blog

Inline rendering with document​.currentScript. Lean Rada describes a neat trick that takes advantage of the 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>

文章评论(12)

雪影28 天前

讲解得很细致,新手也能看懂。

回复
空向阳28 天前

点赞,必须点赞

回复
晨沐雨28 天前

这个比较实用,已转发给同事。

回复
陈皮话梅糖28 天前

点赞,必须点赞

回复
龙文博28 天前

看完了,收获满满,期待更多更新。

回复
郭超28 天前

很有价值的分享,感谢整理。

回复
青柠微凉28 天前

实话,说的不明不白

回复
龙文博28 天前

这个比较实用,已转发给同事。

回复
蒋力28 天前

赞同,实践出真知。

回复
墨向阳28 天前

内容翔实,正好需要,先收藏再看。

回复
风拾光28 天前

实测过类似工具,作者说的基本属实。

回复
墨沐雨28 天前

支持作者,持续关注中。

回复