Chinmay
Chinmay

Reputation: 4912

How do I include external javascript file using thymeleaf?

I'm using Spring Boot / MVC and learning Thymeleaf.

Let's say i have my scripts on another server.

https://staticserver.com/main.js

I've added the '//staticserver.com' portion to my model as jsLocation.

I want to do something on the lines of:

<script src="${jsLocation}/main.js"></script>

which would render

<script src="//staticserver.com/main.js"></script>

Upvotes: 2

Views: 21866

Answers (3)

sangeun jo
sangeun jo

Reputation: 69

if your js file in /resources/static/js/

<script th:src="@{/resources/static/js/fileName.js}"> </script>

Upvotes: 2

Flocke
Flocke

Reputation: 834

<script th:src="|${jsLocation}/main.js|"></script>

Upvotes: 8

Alien
Alien

Reputation: 15908

You can include external js file like below :

<script th:src="source to your external JS"></script> 

You can refer below thread for more details as your query also relates to model.

Thymeleaf external javascript file shares the module attributes with html file

Upvotes: 4

Related Questions