JavaScript API
Once the widget has been installed on your website and has been loaded, we provide an API to talk to the widget for additional functionalities.
Get the widget instance
You can access the Freeday widget custom element:
const freedayWidget = document.querySelector('freeday-widget');
Wait for the widget to be ready
If you want to make sure the Freeday widget is loaded and available, use
freeday:ready to wait for the custom element to be ready:
window.addEventListener('freeday:ready', () => {
document.querySelector('freeday-widget').openChat();
});
Open chat
The widget allows you to open the chat window programmatically. Besides using the default widget button in the bottom-right corner, you can trigger the chat from buttons, links, or other interactive elements.
The openChat method is available on the <freeday-widget> custom element,
after the widget initialisation with freeday:ready.
window.addEventListener('freeday:ready', () => {
document.querySelector('freeday-widget').openChat();
});
Opening with a message
If you want to directly open the chat with a message, you can pass an object
with a message property:
freedayWidget.openChat({ message: 'Hello, I need help with my order' });