Executing JavaScript within the control scope

The Custom action Custom action accepts control as a target window. All user actions are processed and executed within the “control” scope of a transaction. This means that you can access any part of the script from within this scope, and if necessary, modify any property within the steps or actions defined in the script. Script-level settings, however, cannot be modified through custom JavaScript.

When can you use this scripting method?

Use this scripting method if you need to dynamically generate and store content to use somewhere else in the script; for example, to build dynamic date strings for a travel or hotel search.

This method also enables you to access and modify script properties on the step and action levels. The most common scripting scenario is in the case of a conditional step where you tell the agent to ignore certain actions if some condition is met.

Using this method

Instead of entering a window or frame reference as the Target Window, enter control.

Specifying 'control' as the target window

In the Code field, write your custom JavaScript code. Because you are working within the control scope, limit the JavaScript entered here to the following:

  • Modifications to the script properties.

  • Any JavaScript that is not directly related to a target window (for example, creating a dynamic date or random number).

JavaScript that requires access to the page DOM does not belong in the control window. Use the Target Window method to perform any actions against the DOM. For more information, see Executing JavaScript within a target window.

If you use the var keyword when defining a variable within the control scope, it becomes unavailable as soon as the executeJS function (the custom action) completes. Remove the var keyword if you need access to the value later on in the script. It is important to keep this in mind if you are using the control scope to define global variables.

To access that variable later on in the script, you can simply reference the variable name if you reference it within another executeJS; or use the backtick method if you need to insert the value into an action property.

Script, step, and action properties are easily accessible from within the control scope by referencing the element in the JSON model. For example, if you want to access the value of the third action on the second step, write the following:

script.steps[1].actions[2].value