
Run JavaScript Action
The Run JavaScript action allows you to program specific events and to read/set Labvanced variables, but also control objects, in your study by inputting custom JavaScript that will run as an action when triggered to occur.
Table of Contents
Overview
The action is listed under the Code Actions category in the action selection menu.

Common Use Cases
The Run JavaScript action covers logic and interactions that the built-in actions do not.
- Custom calculations: read one or more variables, compute a result in code, and write it back into a variable.
- Editing on-screen elements: with
Native JavaScript, create or change elements on the page directly through the Document Object Model (DOM). - Embedding interactive content: run a JavaScript-based game or custom widget inside a frame, such as the Pong game demo linked below.
How It Works
To access the Run JavaScript action, open the Event System in the Task Editor and create a new event. Under Add Action → Code Actions, select Run JavaScript.
When the above Run JavaScript action is selected, you will see an option for Execution Environment as well the code editor where the JavaScript code should be written (wherein some helpful instructions are located):

| Option | Description |
|---|---|
Execution Environment | Chooses how the code is run: Native JavaScript or Acorn Interpreter. See the section below. |
JavaScript Code | The code editor. It opens with an example that reads and writes a variable called Trial_Nr. Delete the example before writing your own code. |
In both environments, two functions connect your code to Labvanced variables:
| Function | Description |
|---|---|
getValue("Variable_Name") | Returns the current value of the Labvanced variable with that name, for example var my_value = getValue("Variable_Name");. |
setValue("Variable_Name", my_value) | Writes a value into the Labvanced variable with that name. |
If no variable with the given name exists, nothing is read or written, and a warning appears in the browser console.
Execution Environment Options
Here you can specify the nature of the JavaScript you are running, the options include:
Acorn Interpreter: Ideal for scenarios where only variable reading/writing is occurring. The code runs in a sandboxed interpreter without access to the page, andconsole.log,console.warnandconsole.errorwrite to the browser console.Native JavaScript: Enables the execution of custom JavaScript for direct manipulation of the Document Object Model (DOM) within experiments, allowing for the editing of on-screen elements which is useful for integrating interactive JavaScript-based games and creating more dynamic content via JavaScript.
Native JavaScript is the default for a newly added action. An action that was created before the Execution Environment option existed opens with Acorn Interpreter selected.
Note: A study that runs Native JavaScript is run in a separate subdomain for security purposes. When such a study is launched, participants are automatically redirected to https://player.labvanced.com. The Runs Native Javascript Code field in the study's Settings tab (Settings → Main → Settings) reads Yes as soon as at least one Run JavaScript action in the study uses Native JavaScript, and No otherwise.
Utilizing Native JavaScript
When utilizing the Native JavaScript to control elements within the Document Object Model (DOM), one important factor for consideration is knowing what HTML node to attach, as well as to consider some style guides.
One easy ID to consider using is the background identifier and it makes it possible so that other Labvanced objects can still be on the frame as well, such as in the example below:
const container = document.getElementById('background');container.appendChild(gameContainer);
In the examples outlined here, gameContainer is then the main container / div of the custom content and should take on an absolute position for which you need to then set the width and height:
const gameContainer = document.createElement('div'); gameContainer.style.position = 'absolute'; gameContainer.style.width = window.innerWidth +"px"; gameContainer.style.height = window.innerHeight +"px";
The results of such an approach can be seen here in this demo wherein we used the Native JavaScript option to implement the Pong Game in Labvanced: https://www.labvanced.com/page/library/75148
Workflow Tips
- Pick the simpler environment when you can. If the code only reads and writes variables,
Acorn Interpreteris enough. Switch toNative JavaScriptonly when the code needs to touch the page. - Check variable names exactly.
getValueandsetValuelook the variable up by name, so a typo means nothing is read or written. - Debug through the browser console. Use
console.login your code and open the browser's developer console while previewing the study to see the output.
For any questions about general usability of the Run JavaScript action, please feel free to contact us via email or chat.