Script Blocks
HTML Mode supports restricted helper scripts for reusable survey logic. Script helpers are evaluated once into a safe helper surface and can then be used by if, <condition>, <run-code>, <validation>, and Template Expressions.
Helper declaration
<script name="LogicHelpers">
function isTarget(age, gender) {
return age.value >= 18 && gender.o1;
}
registerSurveyHelper("segmentLabel", function(segment) {
return String(segment || "direct").trim().toUpperCase();
});
</script>
<question type="single" name="FOLLOWUP" if="isTarget(AGE, GENDER)">
<description>{{ segmentLabel($params.source) }} segment follow-up</description>
<option value="1">Continue</option>
</question>
Both safe top-level function declarations and registerSurveyHelper(name, function (...) { ... }) are supported.
Helper inputs
Pass question objects or primitive values explicitly.
<question type="single" name="Q1">
<run-code>
if (segmentLabel($params.source) === "PARTNER" && $curr.o1) {
setAnswer("PARTNER_SEGMENT", 1);
}
</run-code>
<option value="1">Partner path</option>
</question>
A helper can read values passed to it, but it must not mutate answers directly. Use <run-code> with setAnswer, clearAnswer, or question-object mutation methods for mutation.
Runtime namespaces and helper inputs
Consuming DSL contexts run alongside the safe runtime helpers documented in Runtime & Logic and can pass those values into script helpers:
Luxon.DateTime,Luxon.Duration,Luxon.Interval$nowwhen the consuming runtime context provides it$paramswhen Survey Link/runtime params are provided$curronly in question-local consuming contexts$statusonly in redirect routeifevaluation
The helper body itself should read its function arguments rather than reaching for $params, $curr, or $status directly. Pass runtime values explicitly, for example segmentLabel($params.source) or isTarget($curr, $now.year).
Safety rules
- Save-time validation checks JavaScript-bearing DSL before persistence.
- Dangerous browser, server, network, storage, dynamic evaluation, timer, and prototype escape surfaces are blocked.
- Local functions, loops, object literals, arrays, and
tryorcatchare allowed when they stay inside the safe runtime policy. - Helper code should stay pure and deterministic. Prefer returning booleans, strings, numbers, or small objects.
- Do not add new global names that collide with reserved runtime names such as
$curr,$now,$params,$status,Luxon,Math, or question names.
Run generate parity
Run generate uses the same helper context as Survey Link simulation. If a helper drives auto-punching or branch logic, include scenario overrides that exercise each branch and verify the generated response/export data.