I have a big chunk of HTML which is a template for some data added using AJAX. I would like to store the chunk in a variable and then do replacements on tokens in it but I don’t want to mess up the formatting of the html to get it into a javascript string. Is there a thing like the
<<END
command in perl which will read everything following into a string until it hits the end token?
No, unfortunately, there is no such construct in JavaScript.
You have a few different options, each with its own merits:
String concatenation
:
Escaping the
newline:
Array joining
:
Storing it in an arbitrary hidden element
:
HTML:
JavaScript:
I prefer using
<script type="text/html">s or HTML comments but<textarea>s seem to be quite popular.Using a full-on templating engine such as
: