# Load the chat widget with JavaScript

Start and remove the Ticketdesk AI chat widget from your own code. Show it only on some pages, after cookie consent or after login, and close it on route changes.

Source: https://ticketdesk.ai/docs/chat-widget-javascript
Updated: 2026-11-22

The standard embed code loads the widget on every page where it runs. If you need more control, load the script without an ID and start the widget yourself with `window.ticketdesk.initChatbot()`.

## How the script starts

The embed script checks for `window.TICKETDESK_ID` when it loads. If it's set, the widget starts right away. If it isn't, the script only defines `window.ticketdesk` and waits for you.

```html
<!-- Starts automatically -->
<script>
  window.TICKETDESK_ID = 'd_xxxxxxxx-s-xxxxxxxxxxxx';
</script>
<script src="https://ticketdesk.ai/ticketdesk-chatbot.min.js" defer></script>
```

Copy your chatbot ID from Settings, Chatbots, in the Embed to Website box of the chatbot card.

## Start the widget from code

Load the script without setting `TICKETDESK_ID`, then call `initChatbot` with the ID when you want the widget to appear:

```html
<script src="https://ticketdesk.ai/ticketdesk-chatbot.min.js" defer></script>
<script>
  window.addEventListener('load', () => {
    window.ticketdesk.initChatbot('d_xxxxxxxx-s-xxxxxxxxxxxx');
  });
</script>
```

`initChatbot` adds a `<div id="ticketdesk-ai">` to the end of the page body and renders the widget into it. The domain rules still apply, so the page must be on one of the chatbot's allowed domains.

## Remove the widget

`initChatbot` returns an object with a `close()` method. Calling it unmounts the widget and removes its container from the page:

```js
const chat = window.ticketdesk.initChatbot('d_xxxxxxxx-s-xxxxxxxxxxxx');

// Later, for example when the user leaves the help section
chat.close();
```

Keep the returned object if you plan to remove the widget later. Call `initChatbot` again to bring it back.

## Show the widget only on some pages

A common setup is to show chat on pricing and help pages but not on the checkout or the blog:

```js
const CHATBOT_ID = 'd_xxxxxxxx-s-xxxxxxxxxxxx';
const showOn = ['/pricing', '/help', '/contact'];

window.addEventListener('load', () => {
  if (showOn.some((path) => location.pathname.startsWith(path))) {
    window.ticketdesk.initChatbot(CHATBOT_ID);
  }
});
```

In a single-page app the page doesn't reload between routes. Call `initChatbot` and `close()` from your router's navigation hook instead, and make sure you never start two widgets at once. The [React and Next.js guide](https://ticketdesk.ai/docs/how-to-add-ai-chatbot-to-react-or-nextjs/525) and [Vue and Nuxt guide](https://ticketdesk.ai/docs/how-to-add-ai-chatbot-to-vue-nuxt/527) show component-based setups.

## Wait for cookie consent

If your consent banner must approve chat tools first, start the widget from the banner's accept callback:

```js
function onConsentGiven() {
  if (!window.ticketdesk) return;
  window.ticketdesk.initChatbot('d_xxxxxxxx-s-xxxxxxxxxxxx');
}
```

The script is still downloaded on page load, but the widget doesn't render or connect until you call `initChatbot`. To avoid loading the script at all before consent, add the `<script>` tag from the callback instead and set `window.TICKETDESK_ID` before it loads.

## Troubleshooting

- `window.ticketdesk` is undefined: the script hasn't loaded yet. With `defer` or `async`, wait for the `load` event before calling it.
- The widget doesn't appear on a staging site: add that domain under Allowed Domains in the chatbot settings.
- The widget starts on every page even though you call `initChatbot` yourself: remove the `window.TICKETDESK_ID` line, or the script starts it automatically on load.

For colors, position, the welcome message and suggestion buttons, see [customize the chatbot](https://ticketdesk.ai/docs/embed-ai-chatbot-on-your-website/507) and [chat suggestions](https://ticketdesk.ai/docs/how-to-add-chatbot-suggestions-to-your-ai-chat-widget/558).
