简体   繁体   中英

How can I use Thymeleaf th:if to call javascript function without onclick?

I am building Login page with Spring boot and thymeleaf. I pass the opcode using model attribute. And I want to use it for th:if to make condition to call javascript function automatically(meaning: without any click). Here is my code under.

<a th:if="${opcode == 1}" th:href="'javascript:setlocation()'"></a>

<script>
    function setlocation(){
        var msg = alert('Loss of location information found. Turning into setting page...');
        location.href='/setlocation';
    }
</script>

Put the th:if on the script itself. For example:

<script th:if="${opcode == 1}">
    alert('Loss of location information found. Turning into setting page...');
    location.href='/setlocation';
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM