Explicit Client-Side Code (Javascript)

Within Activity Language code, you can include your own explicit javascript statements or expressions. Explicit Javascript is denoted by a block of code bracketed the start symbol "JS<" and the end symbol ">JS"  (see Formats below). Exotool automatatically includes explicit javascript as part of the front-end mobile application code. (i.e. client-side code).

Statements vs. Expressions

Explicit javascript code is treated either as a statement or as an expression depending on the context in which it is used.  (see Examples below)

Asynchronous Code

You can also include asynchronous javascript code that will complete at a later point in time in response to events or actions occurring elsewhere in the code. Asynchronous code is denoted by adding the capital letter "P" right after the start symbol such that a block of asynchronous code will be bracketed with "JS<P" and ">JS"  (see Formats below).

Reading Variables

Client-side variables may be accessed (read) in explicit javascript code using the javascript object Env. The javascript expression Env.xxx holds the value of the variable xxx, provided that xxx is on the client side.

Note that Except for returned values of explicit javascript expressions Exotool makes no attempt to determine what variables are either read or written by explicit javascript code. Therefore, there is no guarantee that Env.xxx will contain the value of a user variable. In order to correct this, the user should place the statement "declare_location xxx client before any explicit javascript statement that accesses Env.xxx (see Examples below)

Formats:

JS< ...javascript code...>JS
JS<P ...javascript code...>JS
 

Examples

// set the variable myvar to a structure having some specific components using javascript
mystruct = JS< {name:'Joe', age:55, sex:'Male' } >JS
//set myvar to the number of milliseconds elapsed since January 1, 1970
myvar = JS< Date.now() >JS
//set myvar to the number of milliseconds elapsed since January 1, 1970 with a Promise
myvar = JS< 
new Promise(function(resolve,reject){
	resolve(Date.now());
}) 
>JS
// perform two alerts
 JS<
    alert("One..");
    alert("two..");
 >JS
// perform two alerts with a Promise
 JS<P 
 new Promise(function(resolve,reject) {
    alert("One..");
    alert("two..");
    resolve();
 }) 
 >JS
// perform an alert, wait 5 seconds, then continue
 JS<P 
 new Promise(function(resolve,reject) {
    alert("One..");
    setTimeout(resolve, 5000);
 }) 
 >JS
// read the variable myvar using javascript
declare_location myvar client
alert(JS< Env.myvar >JS)