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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With