1. Embedded
  2. Opening chat programmatically

Embedded

Opening chat programmatically

Overview

You can programmatically open the webchat widget from your webpage by dispatching the stubber_webchat_chat_open event on the window. This is useful when you want to trigger the chat from a custom button, link, or any other user interaction on your page rather than relying on the default webchat launcher.

Usage

Dispatch a stubber_webchat_chat_open event on the window object to open the chat:

        window.dispatchEvent(new Event("stubber_webchat_chat_open"));

      

When this event is fired, the webchat widget will:

  1. Enable the webchat if it is not already enabled
  2. Open the chat window
  3. Establish a connection to the webchat service

Example

Opening chat from a custom button

        <button id="open-chat-btn">Chat with us</button>

<script>
  document
    .getElementById("open-chat-btn")
    .addEventListener("click", function () {
      window.dispatchEvent(new Event("stubber_webchat_chat_open"));
    });
</script>

      

Opening chat based on page behaviour

You can also trigger the chat open event based on other conditions, such as time spent on a page or scroll position:

        // Open chat after 30 seconds on the page
setTimeout(function () {
  window.dispatchEvent(new Event("stubber_webchat_chat_open"));
}, 30000);