Only variable expressions returning numbers or booleans are allowed in this context

Knight Rider :

I am trying to pass a value to my javascript function but that function call depends on a boolean variable. I had this working fine until I recently upgraded to thymeleaf security 5.

This is the code snippet.

<body th:onload="${timerEnabled} ? 'javascript:runTimer(\'' + ${timeRemaining} + '\');'">

timerEnabled has to be true for the function call to be done but thymeleaf now throws an exception as

org.thymeleaf.exceptions.TemplateProcessingException: Only variable expressions returning numbers or booleans are allowed in this context, any other datatypes are not trusted in the context of this expression, including Strings or any other object that could be rendered as a text literal. A typical case is HTML attributes for event handlers (e.g. "onload"), in which textual data from variables should better be output to "data-*" attributes and then read from the event handler. 

How can I resolve this? Thank you.

Knight Rider :

I was able to have it working by using this approach

<body>

<script th:inline="javascript">
    /*<![CDATA[*/

    var flag = [[${timerEnabled}]]; // if timer should be included or not
    var timeRemaining = [[${timeRemaining}]]; // the time remaining.
    window.onload = function() {
        if(!flag)
            return; // Exit/Return if the variable is false
        runTimer(timeRemaining); // Call your favourite method if the variable is true
    };

    /*]]>*/
</script>

Any other approach such as suggested in the exception is appreciated.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Thymeleaf: Only variable expressions returning numbers or booleans are allowed in this context

org.thymeleaf.exceptions.TemplateProcessingException: Only variable expressions returning numbers or booleans are allowed in this context

Assignments are not expressions and only expressions are allowed in this context - Kotlin

"Subqueries are not allowed in this context. Only scalar expressions are allowed."

Assignments are not expressions, and only expressions are allowed in this context - Error when convert Java to Kotlin

Are comparisons really allowed to return numbers instead of booleans, and why?

Powershell - "Expressions are only allowed as the first element of a pipeline"

Only SubQuery expressions that are top level conjuncts are allowed

Is returning only IDs for a JSON API collection allowed?

Variable numbers of groups with Regular Expressions

Python 3.5 - Booleans in expressions

Booleans with numbers in Python

Javascript Input Numbers Only Regular Expressions

Simple Example: Expressions are only allowed as the first element of a pipeline

Basic PowerShell Script Issue: "Expressions are only allowed as the first element of a pipeline"

Basic PowerShell Script Issue: “Expressions are only allowed as the first element of a pipeline”

Modifier static is only allowed in constant variable declarations

modifier 'static' is only allowed in constant variable declarations?

Python datetime only returning negative numbers?

Alexa is not returning the numbers and calculation in the response, only the text?

Returning only whole numbers in MS Access query

Why returning a member variable using "this" pointer is not allowed, but setting a member variable using "this" is allowed?

Returning an array with booleans c++

Why is returning a stack allocated pointer variable in a function allowed in C?

RegEx for numbers with exactly 5 digits and only one allowed leading zero

Why Random variable is only returning 1 character

Why are function expressions not included in the variable object of the Execution Context?

How to store and reuse a combination of selector and context expressions in a single variable?

Mustache: Check for variable in current context only