Inline rendering with document.currentScript
Inline rendering with document.currentScript
内联渲染使用 document.currentScript。Lean Rada 描述了一个巧妙的技巧,利用了 document.currentScript 属性。你可以使用它在页面加载时插入一个微小的内联 <script> 元素,该元素会自行替换:当前年份是 <script> document.currentScript.replaceWith( new Date().getFullYear())</script> 如果你定义一个像这样的快捷函数: $=(...n)=>document.currentScript.replaceWith(...n) 你就会得到一个微小的模板语言:当前年份是 <script>$(new Date().getFullYear())</script> 标签:javascript
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
应来源方要求,这里只提供摘要与原文入口。完整内容请阅读原文。
来源:Simon Willison · simonwillison.net