Unquoting a part of inline JavaScript expression?

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Unquoting a part of inline JavaScript expression?

Min Deokkee
This post was updated on .

Hi. I have a Spring Boot project, and I need to render a part of a script element like the following. (Assume that getMessage function is defined somewhere else.)

Expected output:

<script type="text/javascript">
    /*<![CDATA[*/
    var message = 'Hello, John' + getMessage();
    alert(message);
    /*]]>*/
</script>

where 'John' is a Spring model attribute, say, 'name'.

Is it possible to make the whole 'Hello, John' + getMessage() in a single expression? Is assigning 'Hello, John' and getMessage() to separate JavaScript variables the only way? I tried something like the following but it seems inlined JavaScript expression always gets quoted.

<script type="text/javascript" th:inline="javascript">
    /*<![CDATA[*/
    var message = /*[[|'Hello, ${name}' + getMessage()|]]*/ 'Hello, John good to see you.';
    alert(message);
    /*]]>*/
</script>

Actual output:

<script type="text/javascript">
    /*<![CDATA[*/
    var message = "'Hello, John' + getMessage()";
    alert(message);
    /*]]>*/
</script>

Just in case, I'm using Spring Boot 1.5 which depends on Thymeleaf 2.1.

Regards.