Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does HTML tags work inside script tag?

For example, view-source at Joel Spolsky's public career profile

<script type="text/html" id="stackexchangeanswerswidget">     <h3>Top Answers</h3>      <div class="answers">     </div>  </script>  <script type="text/html" id="topanswer">     <div class="top-answer">         <div class="top-answer-stats">{{= shared.htmlEncode(Score) }}</div>         <span class="top-answer-title"><a href="{{=AnswerLink}}">{{= shared.htmlEncode(Title) }}</a></span>         <a class="add-answer">add</a>         <br class="clear" />     </div> </script>  <script type="text/html" id="answer-view">     <div class="answer">         <div class="answer-stats {{= shared.htmlEncode(Site.toLowerCase().replace(/ /g, '')) }}">             <div class="score">                 <strong>{{= shared.htmlEncode(Score) }}</strong>                 <div class="votecount">votes</div>             </div>             <img class="answer-logo" src="{{= shared.htmlEncode(FaviconUrl) }}" />         </div>         <div class="answer-content">             <span class="q">Q:</span>              <div class="answer-top">                  <a class="answer-title" href="{{= shared.htmlEncode(AnswerLink) }}">{{= shared.htmlEncode(Title) }}</a><br />             </div>              <span class="a">A:</span><div class="answer-body">{{= Body }}</div>              <div class="more-button" style="text-align:center; clear:both; display:none;"><a class="more">More</a></div>          </div>     </div> </script> 

How does HTML tags work inside script tag? and/or What kind of technology used for those html tags, and template alike codes {{= .... }} inside script tags?

like image 870
YOU Avatar asked Apr 15 '11 15:04

YOU


People also ask

Can you put HTML inside a script tag?

Adding JavaScript into an HTML DocumentYou can add JavaScript code in an HTML document by employing the dedicated HTML tag <script> that wraps around JavaScript code. 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.

How does HTML script tag work?

The <script> tag allows code to either be embedded directly into HTML, or included from external script files. Inline scripts are created by placing code between <script> and </script> tags in an HTML file.

Can we use HTML tags in JavaScript?

The HTML <script> tag is used to define a client-side script (JavaScript). The <script> element either contains script statements, or it points to an external script file through the src attribute.

Can I write a script tag inside a script tag?

The usual script tag (type text/javascript) is used to embed js inside markup. The tag itself is not valid js, so you cannot nest script tags (unless it's in an opaque context, such as a string).


1 Answers

You can put anything that you want in a <script> tag. If you specify a content type other than Javascript (or another scripting language that the browser understands), it will not be interpreted by the browser, and you can just access it as plain text. Since the contents of <script> tags are treated as CDATA, the contents are not parsed and you can store unquoted XML or HTML in the contents (as long as you don't ever put a </script> tag in the contents, since that will close your element).

This is used, for example, in SVG Web, a polyfill that allows you to use inline SVG in HTML and have that converted to native SVG in browsers that support it, or Flash in browsers that don't. It allows embedding of SVG in browsers that don't support it natively by wrapping it in a <script> tag, so the browser doesn't try and fail to parse it as HTML.

In the case of SO carreers, it looks like they storing templates for use with Backbone.js and Underscore.js in <script> tags, since that's a convenient place to stick templates in HTML. Here's a snippet of their code where they grab the template and provide it to the Underscore template engine:

    TopAnswerView = Backbone.View.extend({         template: _.template($("#topanswer").html()),         events: {             "click .add-answer": "addAnswerToCV"         }, 
like image 134
Brian Campbell Avatar answered Sep 22 '22 06:09

Brian Campbell