Inline rendering with document​.currentScript

2026-10-18来源 Simon WillisonEnglish

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> 

Tags: javascript

原文:https://www.a8q9.cn/en/article/inline-rendering-with-document-currentscript-d418b9.html

相关文章