Want to try fastn for your company's website?
Book a Demo

Javascript in fastn function

Here is an example of how you can integrate JavaScript in fastn functions.

Suppose we have a JavaScript function show_alert defined in functions.js as follows:
functions.js
function show_alert(a) {
alert(a);
}
Lang:
js
Now, let's say we want to call this function when a user clicks a text in an fastn component. Here's how we can achieve this in index.ftd:
index.ftd
-- ftd.text: Click here to print name in alert!
$on-click$: $call-js-fn(a = FifthTry Alert)

-- void call-js-fn(a):
string a:
js: functions.js

show_alert(a)
Lang:
ftd
In the above example, when the user clicks the text component, the call-js-fn function is called, passing the FifthTry value to the argument a. This function, then, references functions.js by using the js attribute and calls the show_alert function.
Output
Click here to print name in alert!