How:Write scripts in JavaScript for Memento

From Memento Database Wiki
Revision as of 19:22, 24 April 2021 by UnConnoisseur (talk | contribs)
Jump to navigation Jump to search
« Page as of 2021-03-21, editions Mobile 4.10.0, Desktop 1.9.6 »


The JavaScript language

JavaScript is a language developed for programming Web sites. It has also been used as a language embedded within software tools like Memento, most prominently Node.js. The types of Memento JavaScript scripts are:

  • A trigger script, with event & phase options
  • An action script, applying to the current entry or library
  • Shared code for use by trigger & action scripts
  • A custom data source script to guide source data to the appropriate destination fields

JavaScript field

Like the Memento Calculation field type that game before it, the JavaScript field exists to produce ("return") a value calculated with an expression, which is a formula consisting of constants, variables, operators, and possibly statements.

Sample simple expression scripts

the value a hundred and twenty-three
the characters M e m e n t o as a string
2 * currentWeight
If currentWeight is 23 lbs, then 46 lbs
(hours * 60) + 10
If hours is 5 and minutes is 10, then 310

Sample complex expression script

Before release 4.10.0 on the mobile edition & 1.10.0 on the desktop edition, we had to validate entered data with a script like this one:

    // Validate entered value:

    var StdLimit = 100      // Our std for all products

    value = field("Value")  // Fetch value of field
    if (value <= StdLimit)  // If within limit,
      value                 //   Result is the value
    else                    // Otherwise,
      StdLimit              //   Result is the std limit


a value that has a string (text) value or a numeric (number) value.

"This is a string of textual characters"
123 or 3.14 or -28.5
a name representing a constant value, like pi, x, tareWeight, or surname

a name representing a value that may change over time, like sum, currentValue, or orderDate
one or more characters that represent the value of other values that go together in some fashion to determine a result, like + for plus or - for minus or * for multiplied by or / for divided by.
Some operators are made up of other operators used in combination, like <= for less than or equal to or && for and also.

Trigger script


Action script


Shared script


Custom data source script