# Control a storefront survey with JavaScript

Use JavaScript to open, close or refresh a survey on your website. After the Shopoll code loads, these controls are available on `window.Shopoll`.

## Open a survey

With the embed loaded, this handler opens the survey selected for the current placement.

```javascript
document.querySelector('#size-guide-feedback').addEventListener('click', async () => {
  const opened = await window.Shopoll.open();
  if (opened) console.log('The survey is visible.');
});
```

Use the [generated button snippet](/docs/tutorials/custom-trigger.md) when a click might arrive before the embed script has loaded.

| Method | When to use it |
| --- | --- |
| `open()` | Open the current eligible survey. Resolves to true when it is visible. |
| `open(surveyId)` | Request a published survey by its ID. A workspace placement can select a survey belonging to that workspace. |
| `close()` | Close the current open placement. |
| `toggle()` | Close a visible placement or try to open it. |
| `refresh()` | Recheck the placement after page context changes. |

Passing a survey ID lets your website choose the survey. Shopoll then skips its normal page and visitor sampling rules. Publication, scheduling, response allowance, appearance frequency and the applicable [consent settings](/docs/cookie-consent.md) still matter.

## Choose the survey before loading the embed

Set the public survey ID before the installation script runs. The `onload` callback runs when an eligible selection is ready. The iframe may still be loading at this point.

```javascript
window.Shopoll ||= {};
window.Shopoll.pollId = 'YOUR_PUBLISHED_SURVEY_ID';
window.Shopoll.onload = () => window.Shopoll.open();
```

On a later route change, update `pollId` and call `refresh()`. Clear `pollId` and refresh to return a workspace placement to automatic selection.

## Read saved answers

Register callbacks on the storefront that hosts the survey. Set a [question handle](/docs/questions/template-variables.md) in the editor so your code can recognize the question after its wording changes.

```javascript
window.Shopoll ||= {};
window.Shopoll.onAnswer = (answer, responses) => {
  if (answer.slide.handle === 'firstTouch') {
    console.log('Discovery source:', answer.value);
  }
};
window.Shopoll.onClose = responses => console.log('Saved so far:', responses.length);
window.Shopoll.onComplete = responses => console.log('Completed:', responses.length);
```

Each answer has a `slide` with its ID, handle, question title and type. Read `response` for the saved value and `value` for a text representation. Multiple selections also have a `responses` array. Write-in text appears in `writeIn`. Attachments expose file names.

Callbacks receive the saved answers for the questions on the person’s current route through the survey. Closing partway through returns the answers saved up to that point. After a shopper edits a response, callbacks include the updated answers on their new route. The completion callback runs once for that attempt. Hosted survey links and invitations do not send answer content to a storefront callback.
