1. Embedded
  2. Opening chat programmatically

Embedded

Opening chat programmatically

Explains opening embedded Webchat from webpage code using browser events. Useful for custom buttons, page-triggered chats, and guided user journeys.

Overview

Open the webchat widget from your webpage by dispatching the stubber_webchat_chat_open event on the window. Use this to trigger the chat from a custom button, link, or other user interaction instead of the default launcher.

Usage

Dispatch a stubber_webchat_chat_open event on the window object:

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

      

On this event, 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 behavior

Trigger the event from other conditions, such as time on page or scroll position:

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