The script flow: when scripts run

When Connect generates the actual output – letters, web pages or emails -, it opens a record set and merges it with the template. It takes each record, one by one, and runs all scripts for it, in a specific order, as explained below.

First all Control Scripts are executed, in the order in which they appear in the Scripts pane. Control scripts don't touch the content of the sections themselves, but they change the way a template is outputted, for example by selecting or omitting sections from the output (see Control Scripts).

Secondly, any Handlebars expressions are evaluated in sections where the option Evaluate Handlebars expressions is enabled. (On the Resources pane, right-click the section and select Properties to find this setting.)

Then the Standard Scripts are executed, once for each section, in the order in which they appear in the Scripts pane.
Standard Scripts can change the contents of the current section in a template.
This type of script must have a selector: text, an HTML element and/or a CSS selector (see Writing your own scripts and Selectors in OL Connect).
Running a template script starts with looking in the current section for pieces of content that match the script's selector.
Important to note is that if nothing matches the selector, the script is not executed.

In a Print context, the Standard Scripts in the Scripts pane run once for each section and then for each Master Page (see Master Pages). Next, each processed Master Page is put behind every page to which it should be applied.
Scripts are NOT executed again for every page.

Note: Translations are applied before the Standard Scripts run, and also every time a Standard Script adds content to a section, for example with the results.html() function.

Finally, Post Pagination Scripts run, in the order in which they appear in the Scripts pane (see Post Pagination Scripts).
Post Pagination Scripts are run in a Print context after the content has been paginated. Because they can search through the output of all Print sections, and modify Print sections (one at a time), they may be used to create a Table Of Contents (TOC), as explained in the topic: Creating a Table Of Contents.

Tip: Do you find yourself copy-pasting the same function into every new script? You can avoid this by defining your function in a Control Script. Control Scripts are executed first, so the function will then be available in all Standard Scripts and Post Pagination Scripts.

Note: Any JavaScript files included in a section run after the scripts in the Scripts pane.