JavaScript Basics in JourneyApps
JavaScript is used to define the behavior and logic of apps built on JourneyApps.
The following functionality is supported from JavaScript in JourneyApps:
-
The Basics:
- Variable initialisation
- Calling links
- Calling links with branching logic
- Debugging (writing log messages to console)
- Performing calculations
- Working with Objects:
- Other JavaScript Functionality:
JavaScript for Each View
Each view has a corresponding JavaScript file, that is automatically executed when the view loads.
Variable Initialisation
The JavaScript init()
function is called when a view is loaded for the first time. This is typically used for
initialising variables or queries.
Any variable or parameter defined in a view can be referenced by view.variablename
.
Example View XML:
... and the JavaScript:
Calling Links
There are two ways to call links in JourneyApps:
- Directly from a button component by specifying
link=""
on the button. See the Links section for more information. - Calling the link from JavaScript using the
return link.link_name
syntax.
For example, if you have a link defined in your View XML as such:
It can be called from JavaScript as follows:
If the view we are linking to requires any parameters, we can simply pass those parameters to the JavaScript function, for example:
Calling Links with Branching Logic
Example View XML:
... and the View JavaScript:
To do branching, we can simply use instead an if statement in our JavaScript function:
Debugging
When you're using the Test App in Browser feature on the Test page of the App Editor, you can display debugging messages on your web browser's console using the console.log
function:
Performing Calculations
An example for how calculations may be performed:
View XML:
View JavaScript:
Note that the view is automatically refreshed with the new "bmi" and "description" values.
Further JavaScript Functionality in JourneyApps
- Working with Objects:
- Other JavaScript Functionality: