[英]html script tag not using type javascript <script type=“text/html”>?
I was checking out the source on an html page and came across this 我正在检查html页面上的源代码并遇到了这个问题
<script id="searchItemTemplate" type="text/html">
<# var rows = Math.floor((Model.RecordsPerPage - 1) / 3 + 1);
for (var i = 0; i < rows; ++i){
var startIdx = i * 3;
var endIdx = startIdx + 3;
#>
//etc ....
</script>
I have never seen this before. 我以前从未见过这个。 What is script
type="text/html"
. 什么是脚本
type="text/html"
。 I don't know if it makes a difference but this was on a .aspx page. 我不知道它是否有所作为,但这是在.aspx页面上。
Is this some sort of place holder to be parsed and eval() later?
这是某种占位符需要解析和eval()以后吗?
Does anyone know what this is?
有谁知道这是什么?
Can someone who has used this method explain the benefits?
使用这种方法的人可以解释这些好处吗?
Script elements that have an unknown content-type are simply ignored, in this case, the browser doesn't know how to execute a text/html
script. 简单地忽略具有未知内容类型的脚本元素,在这种情况下,浏览器不知道如何执行
text/html
脚本。
It's a common technique used by some JavaScript templating engines. 这是一些JavaScript模板引擎使用的常用技术。
See also: 也可以看看:
It's a trick that I first saw in a John Resig blog post. 这是我在John Resig博客文章中首次看到的一个技巧。 It's used for stuff like holding a template to be expanded later.
它用于举行模板以便稍后扩展的东西。 The browser won't make any attempt to execute it.
浏览器不会尝试执行它。
The "benefits"? 好处”? Well, it's a lot neater than keeping a string in your Javascript code directly.
嗯,它比直接在你的Javascript代码中保留一个字符串要简洁得多。 Because you don't have multi-line strings in Javascript, it's messy to define a template like that.
因为你在Javascript中没有多行字符串,所以定义这样的模板很麻烦。
edit — ha ha yes, CMS has linked the very blog post in his answer! 编辑 - 哈哈是的,CMS在他的回答中链接了博客文章!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.