> For the complete documentation index, see [llms.txt](https://docs.browserflow.app/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.browserflow.app/reference/browserflow-commands/utilities/run-script.md).

# Run Script

Runs Javascript in the current page

#### Code

* Any variable that begins with `$` is automatically exported for use in other commands
  * e.g. `const $meow = 3`
* Variables are serialized as [JSON](https://www.json.org/json-en.html) and thus support the following data types:
  * Primitives (number, string, boolean)
  * Arrays
  * JSON objects

### Examples

#### Replacing Text

```javascript
// Assume variable $numberOfComments has the value "12 comments"
// You want to extract just the numerical count

$numberOfComments = $numberOfComments.replace(" comments", "");

// $numberOfComments is now "12"
```

#### Splitting Text

```javascript
// Assume variable $location has the value "San Francisco, CA"
// You want to split up the city and the state

const [$city, $state] = $location.split(", ");

// $city is "San Francisco", $state is "CA"
```

#### Extracting Page HTML

```javascript
const $html = document.documentElement.outerHTML;
```

### Execution context

The code is executed in the context of the current page which means that the page's DOM is accessible


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.browserflow.app/reference/browserflow-commands/utilities/run-script.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
