I need something like heredoc in JavaScript. Do you have any ideas for this? I need cross-browser functionality.
I found this:
heredoc = \'\\
ES5 and earlier versions
(function(){/** some random multi line text here **/}).toString().slice(15,-5);
ES6 and later versions
`some random multi line text here`
result
some random multi line text here
If you have some html and jQuery at hand and the string is valid HTML, this may be useful:
<div id="heredoc"><!--heredoc content
with multiple lines, even 'quotes' or "double quotes",
beware not to leave any tag open--></div>
<script>
var str = (function() {
var div = jQuery('#heredoc');
var str = div.html();
str = str.replace(/^<\!--/, "").toString();
str = str.replace(/-->$/, "").toString();
return str;
})();
</script>
If text have comments "<!-- -->" in between, it works as well, but a part of the text may be visible. Here's the fiddle: https://jsfiddle.net/hr6ar152/1/