How to insert JavaScript code in a Handlebars template? - Stack Overflow

Quoting Handlebars FAQ:How can I include script tags in my template?If loading the template via an inl

Quoting Handlebars FAQ:

How can I include script tags in my template?

If loading the template via an inlined tag then you may need to break up the script tag with an empty ment to avoid browser parser errors:

<script type="text/x-handlebars">
  foo
  <scr{{!}}ipt src="bar"></scr{{!}}ipt>
</script>

Nevertheless, I can't seem to be able to implement this:

document.getElementById('output').innerHTML = 
  Handlebarspile(
    document.getElementById("template").innerHTML
  )({});
<script src=".js/4.0.10/handlebars.js"></script>

<script id="template" type="text/x-handlebars">
  foo
  <scr{{!}}ipt>
    alert("Yay!");
  </scr{{!}}ipt>
</script>

<div id="output"></div>

Quoting Handlebars FAQ:

How can I include script tags in my template?

If loading the template via an inlined tag then you may need to break up the script tag with an empty ment to avoid browser parser errors:

<script type="text/x-handlebars">
  foo
  <scr{{!}}ipt src="bar"></scr{{!}}ipt>
</script>

Nevertheless, I can't seem to be able to implement this:

document.getElementById('output').innerHTML = 
  Handlebars.pile(
    document.getElementById("template").innerHTML
  )({});
<script src="https://cdnjs.cloudflare./ajax/libs/handlebars.js/4.0.10/handlebars.js"></script>

<script id="template" type="text/x-handlebars">
  foo
  <scr{{!}}ipt>
    alert("Yay!");
  </scr{{!}}ipt>
</script>

<div id="output"></div>

As you can see no Yay! is alerted.

How to make this work?

Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Aug 9, 2017 at 1:11 user4385532user4385532 1
  • It works if I use jQuery .html() though instead of innerHTML – Sandeep Nayak Commented Aug 10, 2017 at 4:55
Add a ment  | 

1 Answer 1

Reset to default 1

I tried the same with jQuery's .html() and it worked fine. I could narrow down on .innerHTML being the culprit when I looked into this answer.

In short, script tags inserted with .innerHTML needs to be explicitly evaluated using eval().

So below code works, if you eval the script:

document.addEventListener('DOMContentLoaded', function() {
  document.getElementById("output").innerHTML = (Handlebars.pile(
    document.getElementById("template").innerHTML
  ))({});

  eval(document.getElementById("myscript").innerHTML); // explicitly eval the script


}, false);
<script src="https://cdnjs.cloudflare./ajax/libs/handlebars.js/4.0.10/handlebars.js"></script>

<script id="template" type="text/x-handlebars">
  foo
  <scr{{!}}ipt id="myscript" src="bar">console.log("Hello")</scr{{!}}ipt>
</script>

<div id="output"></div>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745477288a4629401.html

相关推荐

  • How to insert JavaScript code in a Handlebars template? - Stack Overflow

    Quoting Handlebars FAQ:How can I include script tags in my template?If loading the template via an inl

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信