最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

asp.net - html script tag not using type javascript <script type="texthtml">? - Stack Overflow

programmeradmin4浏览0评论

I was checking out the source on an html page and came across this

<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". I don't know if it makes a difference but this was on a .aspx page.

Is this some sort of place holder to be parsed and eval() later?
Does anyone know what this is?
Can someone who has used this method explain the benefits?

I was checking out the source on an html page and came across this

<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". I don't know if it makes a difference but this was on a .aspx page.

Is this some sort of place holder to be parsed and eval() later?
Does anyone know what this is?
Can someone who has used this method explain the benefits?

Share Improve this question edited Jul 13, 2010 at 20:50 Kenneth J asked Jul 13, 2010 at 20:46 Kenneth JKenneth J 4,89612 gold badges41 silver badges56 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 17

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.

It's a common technique used by some JavaScript templating engines.

See also:

  • JavaScript Micro-Templating
  • JavaScript Templating Engines

It's a trick that I first saw in a John Resig blog post. 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. Because you don't have multi-line strings in Javascript, it's messy to define a template like that.

edit — ha ha yes, CMS has linked the very blog post in his answer!

发布评论

评论列表(0)

  1. 暂无评论