Methods

Complete reference for the JavaScript methods available on the Glassix chat widget client

This page documents the JavaScript methods available for programmatically controlling the Glassix chat widget after initialization, including how to show or hide the widget, retrieve its iframe URI, and safely destroy and reinitialize it across user sessions.

Overview

Before you can trigger any programmatic actions on the Glassix chat widget, the core initialization script must be completely loaded on your website.

To ensure your code executes safely without throwing undefined errors, register your initial method calls inside the globally recognized callback function. This ensures that the widget client instance is fully initialized and ready to receive instructions.

window.glassixWidgetScriptLoaded = function(){
  // now we can call the widget's methods
};

setWidgetVisiblity

Show or hide the widget programmatically.

widgetClient.setWidgetVisiblity(true);
ParameterTypeDescription
visiblebooleantrue to show the widget, false to hide it

See an example of setWidgetVisiblity below

getIframeUri

Returns a promise that resolves to the URI to use as the iframe source. Use this method if you want to embed the chat in your own iframe instead of the default widget.

widgetClient.getIframeUri().then(function (uri) {
  var iframe = document.getElementsByTagName("iframe")[0];
  iframe.src = uri;
});

Alternatively, you can embed the chat in your own iframe without using this method. See Custom Installation for details.

destroy

Removes the widget instance from both the DOM and memory.

widgetClient.destroy();

A common use case is to destroy and reinitialize the widget when a user logs in or out, so the widget can be rebuilt with an updated identity or session context.

Example: reinitializing the widget after login:

function reinitializeGlassixWidget() {
  var newWidgetOptions = {
    apiKey: "your-api-key",
    snippetId: "your-snippet-id",
  };

  // Add customer context if the user is signed in
  if (currentCustomer) {
    newWidgetOptions.uniqueIdentifier = currentCustomer.customerUuid;
    newWidgetOptions.identityToken = authToken;
  }

  // Destroy the existing instance
  if (window.widgetClient && typeof window.widgetClient.destroy === 'function') {
    window.widgetClient.destroy();
  }

  // Reinitialize with updated options
  if (window.GlassixWidgetClient && typeof window.GlassixWidgetClient === 'function') {
    window.widgetClient = new GlassixWidgetClient(newWidgetOptions);
    window.widgetClient.attach();
  }
}

See below an example of how the widget gets destroyed (the widget briefly disappears) on page reload.

attach

Attaches the widget to the DOM. This method is called automatically when the script loads; you do not need to call it explicitly unless you are manually reinitializing the widget after a destroy() call.

widgetClient.attach();

Example: full initialization script using attach explicitly:

<!-- Start of Glassix Chat Widget -->
<script>
  var widgetOptions = {
    apiKey: "your-api-key",
    snippetId: "your-snippet-id",
  };

  // Attach identity context if the user is already signed in
  if (loadCustomerFromStorage()) {
    widgetOptions.uniqueIdentifier = currentCustomer.customerUuid;
    widgetOptions.identityToken = authToken;
  }

  (function (n) {
    var u = function () {
      GlassixWidgetClient && typeof GlassixWidgetClient == "function"
        ? (window.widgetClient = new GlassixWidgetClient(n),
           widgetClient.attach(),
           window.glassixWidgetScriptLoaded && window.glassixWidgetScriptLoaded())
        : f();
    },
    f = function () {
      r.onload = u;
      r.src = "https://cdn.glassix.net/clients/widget.1.2.min.js";
      i.parentNode.removeChild(t);
      i.parentNode.insertBefore(r, i);
    },
    i = document.getElementsByTagName("script")[0],
    t = document.createElement("script"),
    r;
    (t.async = !0,
     t.type = "text/javascript",
     t.crossorigin = "anonymous",
     t.id = "glassix-widget-script",
     r = t.cloneNode(),
     t.onload = u,
     t.src = "https://cdn.glassix.com/clients/widget.1.2.min.js",
     !document.getElementById(t.id) && document.body) &&
    (i.parentNode.insertBefore(t, i), t.onerror = f);
  })(widgetOptions);
</script>
<!-- End of Glassix Chat Widget -->

If you are reinitializing the widget to pass the updated identity context, see Identity Verification for instructions on generating the required uniqueIdentifier and identityToken.