Skip to content
Advertisement

How to include JavaScript file inside a Velocity template?

I have a JS file having contents: function rejectIfNotSuccess(response) {} usernameChanged(event) {} for example, and I want to include this file in my velocity template in order to do things the same way we do in an HTML file.

For example, I want to call a function defined in this JS file in velocity using <input type="text" id="username" onInput="javascript:usernameChanged(event)" />

How can I do so? I read here about a way to do so, which may work. But, are there any other better ways of doing the same?

Thanks Stephen for your answer!

Advertisement

Answer

Basically you do it that same way that you would do it if you were writing the HTML file by hand. Embed an appropriate <script> element into the velocity template that generates that HTML.

(The <script> element could contain the function declaration, or it could use a src link to load a document containing the Javascript from your server. See https://www.tutorialsteacher.com/javascript/script-tag for examples.)

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement