> ## Documentation Index
> Fetch the complete documentation index at: https://crossword.ai/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Add an Ask AI button

> Turn any button on your site into an Ask AI button that opens the Crossword chat, and optionally asks a question for the shopper.

Any button on your site can open the Crossword chat. It can also ask a question for the shopper, such as "Help me pick the right product".

The chat widget must already be on your site. If it is not, add your environment's [installation snippet](https://crossword.ai/docs/orchestration/deployment.md#installation) first.

## Add the button

**1. Add the helper script once**

Add this script to every page, for example in your theme's `<head>`. Add it once for the whole site.

```html theme={null}
<script>
  window.Crossword =
    window.Crossword ||
    function () {
      (window.Crossword.q = window.Crossword.q || []).push(arguments);
    };

  function askAI(question) {
    var agent = window.Crossword;
    var call =
      typeof agent === "function"
        ? function (method, arg) { agent(method, arg); }
        : function (method, arg) { agent[method](arg); };
    call("open");
    if (question) call("sendMessage", question);
  }
</script>
```

The first part makes the button safe to click before the widget finishes loading. An early click is saved and runs once the widget is ready. `askAI()` works before and after the widget loads.

**2. Add the button**

To open the chat only:

```html theme={null}
<button type="button" onclick="askAI()">Ask AI</button>
```

To open the chat and ask a question for the shopper:

```html theme={null}
<button type="button" onclick="askAI('Help me pick the right product')">Ask AI</button>
```

The question appears in the chat as the shopper's message, and your Agent replies.

**3. Style the button**

It is a normal button, so style it with your own theme classes or CSS. The widget does not style it.

> [!TIP]
>
> To show only your own button, turn on **Hide the launcher** under **Launcher visibility** in the widget settings. The closed widget then stays hidden until your button opens it.

**4. Test the button**

1. Load a page and click the button. The chat opens.
2. Hard-refresh the page and click the button straight away. The chat opens once the widget is ready.
3. If your button asks a question, let the page finish loading, then click it. The question appears as the shopper's message, and your Agent replies.

## Available functions

`askAI()` uses these functions. You can also call them from your own code.

| Function | What it does |
| - | - |
| `Crossword.open()` | Opens the chat. |
| `Crossword.close()` | Closes the chat. |
| `Crossword.toggle()` | Opens the chat if it is closed, and closes it if it is open. |
| `Crossword.sendMessage(text)` | Sends `text` as the shopper's message. |
| `Crossword.setContext({...})` | Passes details about the page to your Agent, such as the product the shopper is viewing. |

> [!NOTE]
>
> Pages that already use `CrosswordAgent` keep working. No change is needed.

To sign shoppers in and out from your site, read [Shopper identity](https://crossword.ai/docs/identity/overview.md).

## When the button does nothing

* **The page is hidden by your page rules.** If your page rules hide the widget on a page, the button does nothing there. The click is not saved for later.
* **The chat is waiting for cookie consent.** If you turned on **Also block chat until consent is given**, the button does nothing until the shopper accepts cookies. Clicks before that are not saved.
* **A capture sheet is due.** If a capture sheet opens on the shopper's first message, the question waits. It is sent once the shopper finishes the sheet.
* **Your page already uses the name `Crossword`.** The widget always takes the name `window.Crossword`. If your page has its own `Crossword`, such as a script variable or an element whose `id` or `name` is `Crossword`, early clicks are lost. Rename yours.
* **The shopper clicked before the chat was ready.** The chat opens, but the question may not be sent. The shopper can type it, or click the button again.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.