Control the chat widget with JavaScript
Open, close or toggle chat from your own buttons, listen for events, change the language and remove the widget, using the window.Helpdesk API.
Last updated 2 min read
The chat widget gives your page a small JavaScript API at window.Helpdesk. Use it to open chat from your own buttons, react when the chat opens, tell the widget about page changes, switch language or remove the widget completely.
Before you start
- Who can do this: a developer or anyone who edits your site's code.
- Plan: all plans.
- What you need: the chat snippet installed on the page.
- Time: a few minutes per change.
Step by step
- Make sure the snippet from Settings > Chat widget > Install is on the page.
- Add this small helper once. It calls the widget if it has loaded, and otherwise queues the call for the loader to replay:
function hd(method, ...args) {
const h = window.Helpdesk;
if (h && typeof h[method] === 'function') h[method](...args);
else (window.Helpdesk = h || { _q: [] })._q.push([method, ...args]);
}
- Call the method you need, for example on a "Contact us" link:
document.querySelector('#contact-us').addEventListener('click', (e) => {
e.preventDefault();
hd('open');
});
- Test the page in a private window.
What happens next
Methods run as soon as the widget is ready. Calls queued in Helpdesk._q before that are replayed in order when the loader starts.
Available methods
| Method | What it does |
|---|---|
open() |
Opens the chat, loading the window first if needed |
close() |
Hides the chat |
toggle() |
Opens or hides the chat |
identify({ email, name, externalId, phone, hash }) |
Tells the widget who the visitor is. See Identify logged-in shoppers |
page() |
Reports a page change in single-page apps |
cart({ items, subtotalCents, currency, … }) |
Sends the visitor's cart to Hushdesk |
setLocale('fr') |
Switches the widget language |
on(event, callback) |
Listens for ready, open, close, unread or message; returns an unsubscribe function |
track(name, props) |
Passes a named event to your message listeners only |
destroy() |
Removes the button, the chat window, every listener and window.Helpdesk |
Cart and page in the inbox. cart() and page() are stored on the visitor's chat session today, but the inbox does not show them to agents yet. Showing the visitor's current page and cart next to a chat is coming.
Tips
- Add a "Chat with us" link in your footer or contact page that calls
open(), so shoppers find chat where they already look. The corner button stays too; set its position and size under Customise the chat button. - Use
on('unread', ({ count }) => …)to show an unread badge on your own button. destroy()is useful when a visitor opts out of chat in your privacy settings. Reload the page to bring the widget back.
Troubleshooting
"Helpdesk.open is not a function"
Your code ran before the loader, which uses defer. Use the hd helper, which queues the call until the widget is ready.
open() does nothing
Check that the page is an allowed site. On other sites, the chat window shows This chat is not available on this site. See Choose which sites can show chat.
Events fire twice
Your listener was added twice, for example on each route change. Keep the function on() returns and call it to remove the old listener.
Frequently asked questions
How do I open Hushdesk chat from my own button or link?
Call Helpdesk.open() from your button's click handler, for example on a Contact us link. Helpdesk.close() hides the chat again and Helpdesk.toggle() switches between the two. If the chat window has not loaded yet, open() loads it first, so the call always works.
Can I run code when a visitor opens the chat?
Yes. Use Helpdesk.on('open', callback) to run code when the chat opens, and on('close') when it closes. You can also listen for ready, unread with the unread count, and message for named widget events. Each on() call returns a function that removes the listener.
Do Helpdesk calls work before the widget has loaded?
Yes, if you queue them. The loader uses defer, so your inline script often runs first. Push calls onto window.Helpdesk._q as a method name followed by its arguments, and the loader replays them in order when it starts. The small helper in this guide does that for you.
Does Helpdesk.track send events to my Hushdesk reports?
No. In this version track only passes the event to your own message listeners on the page and is not sent to the server. Use it to connect the widget to your analytics code. Page views from page() and carts from cart() are sent to Hushdesk.
How do I tell the widget about a page change in a single-page app?
Call Helpdesk.page() after each route change. Single-page apps do not reload, so without it the widget keeps the first page's address. Hushdesk stores the latest page on the visitor's chat session so it reflects where they are now.
Was this helpful?
Related articles
- Identify logged-in shoppers in chatTell the chat widget who a logged-in shopper is with Helpdesk.identify, so chats land on the right customer. Learn what verified identity adds.
- Customise the chat buttonChange the chat launcher's icon, label, size and corner, move it away from the edges and lift it on phones so it never covers your cart bar.
- Chat widget languagesThe chat widget speaks 12 languages and picks the visitor's browser language automatically. Learn which languages are included and how to switch.
- Remove the chat widget from your siteTake Hushdesk chat off your site by deleting the snippet or the plugin. Nothing is left behind, and your tickets and settings stay in Hushdesk.
Still stuck?
Chat with the Hushdesk team. A person answers on every plan.