JavaScript Code Execution

 
There are some points to be aware of when adding any controls that execute JavaScript (Calculate, JavaScript, and JavaScript - Button controls) to a Page.
 

Execution Order

Controls on the Page that contain JavaScript are executed in a particular order.
 
Upon Page Load
Upon a Page being loaded, all JavaScript controls have their content executed, starting at the top-left-most control on the Page, and finishing at the bottom-right-most. It should be noted that JavaScript Fields are executed prior to the initialisation of Fields or System and Workflow Variables, and attempting to read from/write to any Fields or Variables at this stage will fail.
 
After this, all Calculate controls on the Page have their content executed. They execute from the top-left to the bottom-right, just as JavaScript controls do.
 
Finally, any JavaScript - Buttons set to Auto-Click will have their content executed. They execute from the first JavaScript - Button placed in the Workflow to the last (by ascending order of Field ID). If Run Calculations is enabled for them, then refer to the section below.
 
 
When a Field Value Changes/Run Calculations
When a Field on the Page has its value changed (with one exception), or a JavaScript - Button with Run Calculations enabled completes, then all Calculate controls on the Page have their code executed. They execute from the top-left to the bottom-right.
 
The exception to calculations being run (or even visually updating the Page) when a Field's value is changed is when an event listener's callback function updates a Field's value. This can lead to the mistaken perception that the listener has failed to trigger.
 
Neither JavaScript controls nor JavaScript - Buttons will execute automatically as a result of a Field's value changing, although JavaScript - Buttons may be manually triggered by the code executed by a Calculate, and functions defined in JavaScript controls may be called by the code executed in a Calculate.
 
 
When a JavaScript - Button is Clicked
When a JavaScript - Button is clicked, its code will be executed. If it has Run Calculations enabled, then upon its completion it will trigger any Calculate controls on the Page to execute their code as described above. If not, then no further action is taken.
 
 

Code Scope

JavaScript code defined in different controls can only be accessed and used by certain other controls on the Page:
 
 
Accessible From
Defined In
Calculate
JavaScript
JavaScript - Button
Calculate
JavaScript
JavaScript - Button