Skip to content

Class: DomService ​

Utility service for accessing the app/widget page's DOM: shortcuts to <head>/<body> and dynamic loading of external scripts.

Remarks ​

Available everywhere via k.dom. You never have to instantiate this service yourself — it is created by the SDK when the app/widget starts.

Example ​

ts
// Load a third-party library before using it
await k.dom.loadScript('https://cdn.example.com/confetti.min.js');
k.dom.body.classList.add('ready');

Implements ​

Properties ​

body ​

ts
body: HTMLElement = document.body;

The <body> element of the app/widget document.


ts
head: HTMLElement = document.head;

The <head> element of the app/widget document.

Methods ​

loadScript() ​

ts
loadScript(
   url, 
   element?, 
id?): Promise<void>;

Dynamically loads an external script into the page, avoiding duplicates.

Parameters ​

url ​

string

URL of the script to load.

element? ​

HTMLElement

Parent element into which the <script> tag is inserted.

id? ​

string

Identifier of the <script> tag, also used as a deduplication key: if an element with this id already exists, the script is not reloaded.

Returns ​

Promise<void>

A promise that resolves once the script has loaded, and rejects on a load error. Resolves immediately (doing nothing) if the script is already present.

Default Value ​

`head` (the document's <head>)

Default Value ​

the script's url

Remarks ​

The parameter order is (url, element, id); element and id are optional.

Example ​

ts
await k.dom.loadScript('https://cdn.example.com/lib.js', k.dom.body, 'lib');