Javascript on a script page



  • As per this:
    https://playground.jsreport.net/studio/workspace/SkM6uUaeQ/2

    How do I call a javascript function that has been defined in <script> tags?

    Thanks!

    edg


  • administrators

    hi! you can't, at least not in the way that you are intended, you can't use a function defined in your template (script tag) as a handlebars helper, it is a different context (server side vs browser side), if you need to use something as handlebars helper you should put it in the same place that your add function.

    however you can use the sum function inside the browser context (the script tag) to get a result, and then you can use that result to manipulate content in your page using the DOM



  • How do I access the DOM? I can't seem to use document.getElementById for example.

    updated the playground. (https://playground.jsreport.net/studio/workspace/SkM6uUaeQ/3)


  • administrators

    try to think about the execution context of the functions you have.

    your add function, which is defined as template helpers is executed in server side context, it does not have access to the DOM, that is why everything you define in template helpers can be called as handlebars helpers.

    your sum function, which is defined as javascript function in page is executed in browser side context as a normal browser that loads a page would do. it can not access handlebars helpers, you can modify DOM here because you are in browser side context

    remember that jsreport executes your template to get final html (generated by your template body, helpers calls and data), and then uses that html to evaluate it in a recipe which usually is a browser (phantom, chrome), so in that part everything you have as script tags inside your html is evaluated


Log in to reply
 

Looks like your connection to jsreport forum was lost, please wait while we try to reconnect.