Can I use thymeleaf syntax inside of javascript code?

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();
    });
});