How do I pass a Loadster variable into the browser so the page can read it?
Loadster variables, like dataset values you grab with bot.getVariable(), live outside the browser, in your code
blocks. Sometimes you want one of those values available inside the browser, so an
Evaluate Block or the page’s own JavaScript can pick it up.
The cleanest way to do this is browser.setVariable(name, value) in a code block. It assigns
the value directly to the browser’s window object, where it’s available globally on the current page and persists
across navigations within the same bot.
Using browser.setVariable
Here’s a typical pattern: pull a value from a dataset, push it into the browser, then navigate to a page that uses it.
// Grab a token from the dataset (or wherever else you have one)
const apiToken = bot.getVariable("apiToken");
// Push it onto window in the browser
browser.setVariable("apiToken", apiToken);
// If you navigate the variable persists across the navigation
browser.navigate("https://example.com/dashboard");
// Subsequent eval blocks (or scripts on the page) can read it
const echoed = browser.eval("window.apiToken");You can pass any JSON-serializable value, including arrays and objects:
browser.setVariable("testConfig", {
userId: bot.getVariable("userId"),
features: ["beta", "experimental"],
iteration: bot.getIteration()
});Why not just use browser.eval?
You can absolutely build an eval() string yourself, but it gets messy. You may have to JSON-encode the value or
worry about escaping quote characters, and the resulting code is harder to read. browser.setVariable() skips all
that and hands the value over directly.
What’s supported
browser.setVariable() accepts the same kinds of values that JSON understands: strings, numbers, booleans, arrays,
plain objects, and null. Functions, DOM nodes, and class instances can’t be passed through, since they don’t survive
serialization.
The variable is set on window in the current page right away, and it’s also re-applied on every navigation within the
same bot’s browser session, so you don’t have to set it again after each browser.navigate(). The variable is gone
once the bot’s browser session ends, which usually means the end of the iteration (unless you’ve disabled
clearing state between iterations).