hiw3c 日报精选
Simon Willison 网页快照

使用 document.currentScript 进行内联渲染

原文标题 · Inline rendering with document​.currentScript

使用 document.currentScript 进行内联渲染。Lean Rada 提供了一种巧妙的方法,可以利用 document.currentScript 属性来实现这一功能。你可以插入一个小的内联 <script> 元素,在页面加载时自动替换原来的代码:例如:`<script>document.currentScript.replaceWith(new Date().getFullYea

来源:Simon Willison · simonwillison.net

正文为英文,可一键机器翻译(仅首次需要等待)

Simon Willison’s Weblog

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)
The year is <script>$(new Date().getFullYear())</script>

Recent articles

Hello Python 3.15, goodbye 3.10, and now I can use 3.11 - 12th October 2026

A new feature for my blog, built using my voice - 9th October 2026

Claude Haiku 5.5 - 7th October 2026

We're going to need default hard budget caps on pretty much everything - 3rd October 2026

This is a link post by Simon Willison, posted on 17th October 2026 .

Monthly briefing

Sponsor me for $10/month and get a curated email digest of the month's most important LLM developments.