Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to put code thymeleaf in an external javascript file?

I have an external javascript file which is declared in my html file with the following tag:

<script type="text/javascript" th:inline="javascript" th:src="@{/js/gp-aprobarDocumento.js}"></script>

and in gp-aprobarDocumento.js the code shown below:

ventanaAprobacion = function(td) 
{
  /*<![CDATA[*/
    idEntregable = $(td).attr("data-row-id");
    idVersion = $(td).attr("data-row-version");
    alert("la siguiente viene con el texto dle properties");
    alert(/*[[${link.menu.page-certificacion-qa-bandeja-entrada}]]*/);
    $(function() {
        $("#dialog-aprobar-documento").dialog("open");
    });
  /*]]>*/
}

Thus when executed the function the window alert is shown empty.

Does somebody know how to put thymeleaf expression in a external javascript?

like image 420
Premier Avatar asked Nov 27 '14 18:11

Premier


People also ask

How do I add external js in Thymeleaf?

You can't put Thymeleaf template code in an external JavaScript file, but you can put JavaScript code in a pair of <script> tags in a html template, using a template fragment in the HTML.

Can you use Thymeleaf in JavaScript?

Thymeleaf is a modern server-side Java template engine for both web and standalone environments, capable of processing HTML, XML, JavaScript, CSS and even plain text. The main goal of Thymeleaf is to provide an elegant and highly-maintainable way of creating templates.

How do I write an external JavaScript file?

To include an external JavaScript file, we can use the script tag with the attribute src . You've already used the src attribute when using images. The value for the src attribute should be the path to your JavaScript file. This script tag should be included between the <head> tags in your HTML document.

Can external JavaScript contain script tag?

External Javascript should not contain tag. Show activity on this post. No, <script> tags are not needed otherwise error occurs. For example, external.


1 Answers

I think what you want to do it's not possible, I have a similar question (here:How do you access a model attribute with javascript variable)

but in your case you can do something like the this:

in html:

<script type="text/javascript" th:inline="javascript" >
    var alertVariable = ${link.menu.page-certificacion-qa-bandeja-entrada};
</script>

and in the javascript the following:

ventanaAprobacion = function(td) 
{
    ...
    alert(alertVariable);
    ...
}

I know that's not really what you want but I have the same problem and I don't think there is any solution.

like image 69
Manu Zi Avatar answered Oct 05 '22 16:10

Manu Zi