4

I'm developing a Spring Boot web application with Thymeleaf as the template engine.

I want to call a JavaScript function as soon as the page loads, if a specific attribute is present in the Model.

I did some research and I found a way to call a function but with clicking on a span etc., which doesn't do the job for me.

Thanks in advance.

1 Answer 1

7

You basically want to call a Javascript method depending on a Thymleaf variable as soon as the page loads.

Try this

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

    var flag = [[${flag}]]; //Your Thymleaf variable
    window.onload = function() {
        if(!flag)
            return; // Exit/Return if the variable is false
        spreadLove(); // Call your favourite method if the variable is true
    };

/*]]>*/
</script>
Sign up to request clarification or add additional context in comments.

2 Comments

Sorry for my late answer, it works just as i wanted to. Thanks a lot!
Great answer bro

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.