Preinitialize the widget JavaScript object
Calling Kapa's JavaScript functions, and listening to Kapa events, requires
that the the global Kapa
object is initialized. Preinitialization lets you
call functions and listen to events immediately, regardless of the lifecycle
state of the Kapa
object. Calls invoked before the Kapa
object has loaded
are queued, and execute as soon as Kapa has completed initialization.
Setup
To preinitialize the window.Kapa
object, copy and paste the JavaScript
snippet below inside a <script>
tag on every page where you are currently
rendering the Kapa widget.
(function () {
let k = window.Kapa;
if (!k) {
let i = function () {
i.c(arguments);
};
i.q = [];
i.c = function (args) {
i.q.push(args);
};
window.Kapa = i;
}
})();
We recommend putting this script as early as possible in the DOM, such as
within the page's <head>
element:
<head>
<script>
(function () {
let k = window.Kapa;
if (!k) {
let i = function () {
i.c(arguments);
};
i.q = [];
i.c = function (args) {
i.q.push(args);
};
window.Kapa = i;
}
})();
</script>
</head>
Usage
The syntax for calling functions against a preinitialized Kapa object is
slightly different to the regular function calls. For example, the regular
syntax for calling the open()
function to open the Kapa widget looks like
this:
window.Kapa.open({ query: "how to get started" });
Whereas with a preinitialized object, you would call the function as follows:
window.Kapa("open", { query: "how to get started" });