Tom
Tom

Reputation: 83

Can I use thymeleaf syntax inside of javascript code?

Here is the part of the code that I try to take out to .js file

$(document).ready(function () {
$("#buttonCancel").on("click", function () {
    window.location = "[[@{/users}]]"; 
});
});

and link to this file from .html right before close body tag:

<script type="text/javascript" th:src="@{/js/userForm.js?v=1}"></script>

If I click "Cancel" I have an error 400. Yes, I can use "window.location = "/myweb/users";" and it works well. But I am curious if there any way to use th-syntax in javascript file?

Upvotes: 0

Views: 70

Answers (1)

trincot
trincot

Reputation: 350137

As alternative, you could create a hidden link in your HTML, and then use it in your script:

HTML:

<a id="cancelAction" style="display:hidden" th:href="@{/users}"></a>

JavaScript:

$(document).ready(function () {
    $("#buttonCancel").on("click", function () {
        $("#cancelAction").click();
    });
});

Upvotes: 1

Related Questions