Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

<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?

like image 358
Kenneth J Avatar asked Jul 13 '10 20:07

Kenneth J


People also ask

How do I run a script tag in HTML?

The “script” tag JavaScript programs can be inserted almost anywhere into an HTML document using the <script> tag. You can run the example by clicking the “Play” button in the right-top corner of the box above. The <script> tag contains JavaScript code which is automatically executed when the browser processes the tag.

Can you put HTML in script tag?

The <script> tag can be placed in the <head> section of your HTML or in the <body> section, depending on when you want the JavaScript to load.

Does script tag require type?

The type attribute in JavaScript is optional since the introduction of HTML5 brought some new improvements. JavaScript became the default language for HTML5 and modern browsers. So, now adding text/javascript isn't required in <script> tag.


1 Answers

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
like image 89
Christian C. Salvadó Avatar answered Oct 02 '22 09:10

Christian C. Salvadó