# Execute Javascript

Source: https://docs.scrappey.com/docs/execute-javascript

> Run custom JavaScript code directly within the page context during a scraping session.

## Command

`"execute_js"`

This action injects and runs your JavaScript code inside the target page, just like a script executed from the browser's console.

## Parameters

| Parameter         | Type    | Required | Description                                                                                       |
| ----------------- | ------- | -------- | ------------------------------------------------------------------------------------------------- |
| `code`            | string  | Yes      | The JavaScript code to execute. This must be a valid JavaScript string.                           |
| `dontReturnValue` | boolean | No       | Set to `true` if the script has no return value. By default the evaluated result is captured.     |

## Return Values

The result of each `execute_js` action is pushed onto the `javascriptReturn` array (in execution order). You can reference a previous result inside a later action using the `{javascriptReturn[n]}` placeholder, where `n` is the zero-based index:

```json
{
  "browserActions": [
    {
      "type": "execute_js",
      "code": "document.querySelector('.token').dataset.value"
    },
    {
      "type": "type",
      "cssSelector": "#input",
      "text": "{javascriptReturn[0]}"
    }
  ]
}
```

## Example

```bash
curl -X POST "https://publisher.scrappey.com/api/v1?key=YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "cmd": "request.get",
    "url": "https://example.com",
    "browserActions": [
      {
        "type": "execute_js",
        "code": "console.log('\''Hello, world!'\'');"
      }
    ]
  }'
```

## Notes

Use Case Ideas:

* Extract values or text content from the DOM.
* Trigger JavaScript events like clicks or input changes.
* Interact with pages that use frameworks like React or Vue by invoking component methods.
* Modify the DOM before parsing the response.

Tips:

* Avoid long-running loops or infinite scripts — they may cause timeouts.
* Escape special characters properly when sending code inside JSON.
* You can combine this with `if`, `scroll`, `click`, or `type` for dynamic control flows.

## Related concepts

Go deeper in the [Scrappey knowledge base](https://scrappey.com/qa):

- [Dynamic content scraping](https://scrappey.com/qa/web-automation/dynamic-content-scraping) — running JS to reach content
- [Headless browser](https://scrappey.com/qa/web-scraping-apis/what-is-a-headless-browser) — the engine that runs your JS
