Skip to main content

Javascript API

The Consent Prompt JavaScript API is a set of methods and events that can be used to interact with the Consent Prompt. The following sections provide an overview of the available methods and events.

Methods

All these methods are available on the window.Osano.prompt object from within the browser after the Consent Prompt has been loaded.

addEventListener

/**
* Adds an event listener for the specified event type.
*/
addEventListener(type: string, listener: EventListener): void

The addEventListener method allows you to listen for events that occur during the user's interaction with the Consent Prompt. Please refer to the Events documentation for a list of available events.

hasConsented

/**
* Returns true if the user has consented to the protocols
* in the first or only Consent Prompt on the page.
*/
async hasConsented(): Promise<boolean|null>

/**
* Returns true if the user has consented to the protocols in the
* Consent Prompt with the specified path in the form of <customerId>/<configId>.
*/
async hasConsented(path: string): Promise<boolean|null>

/**
* Returns true if the user has consented to the protocols in the
* Consent Prompt with the specified <customerID> and <configID>.
*/
async hasConsented(customerId: string, configId: string): Promise<boolean|null>

The hasConsented method returns a Promise that resolves to true if the user has consented to any protocols in the Consent Prompt, and false if they have not submitted any selections. The method will return null if the Consent Prompt is not yet loaded.

This method is useful for determining if the user has interacted with the Consent Prompt and made a selection.

warning

The return value of the hasConsented method is not a guarantee that the user has consented to all protocols. It only indicates that the user has made a selection and does not guarantee that the selection was to accept.

hide

// Hides the first or only Consent Prompt on the page.
async hide(): Promise<boolean>

// Hides the Consent Prompt with the specified path in the form of <customerId>/<configId>
async hide(path: string): Promise<boolean>

// Hides the Consent Prompt with the specified <customerID> and <configID>.
async hide(customerId: string, configId: string): Promise<boolean>

The hide method hides the Consent Prompt from the user. This method returns a Promise that resolves to true if the Consent Prompt was successfully hidden, and false otherwise.

There are a few reasons why the Consent Prompt may not be hidden:

  • The Consent Prompt is not currently displayed.
  • The Consent Prompt is in the process of submitting.

load

// Loads the first or only Consent Prompt on the page.
async load(): Promise<void>
// Loads the Consent Prompt with the specified path in the form of <customerId>/<configId>
async load(path: string): Promise<void>
// Loads the Consent Prompt with the specified <customerID> and <configID>.
async load(customerId: string, configId: string): Promise<void>

The load method loads the Consent Prompt into the page. This method returns a Promise that resolves when the Consent Prompt has been successfully loaded.

You do not need to call this method unless you are using the Consent Prompt in a Single Page Application (SPA) or a similar environment where the Consent Prompt is not loaded with the page, or you are loading the Consent Prompt dynamically without using additional script tags.

removeEventListener

/**
* Removes an event listener for the specified event type.
*/
removeEventListener(type: string, listener: EventListener): void

The removeEventListener method allows you to remove an event listener that was previously added using the addEventListener method.

show

// Shows the first or only Consent Prompt on the page.
async show(): Promise<boolean>

// Shows the Consent Prompt with the specified path in the form of <customerId>/<configId>
async show(path: string): Promise<boolean>

// Shows the Consent Prompt with the specified <customerID> and <configID>.
async show(customerId: string, configId: string): Promise<boolean>

The show method displays the Consent Prompt to the user. This method returns a Promise that resolves to true if the Consent Prompt was successfully displayed, and false otherwise.

There are a few reasons why the Consent Prompt may not be displayed:

  • The Consent Prompt has no protocols to be shown for the current user's jurisdiction.
  • The Consent Prompt does not exist or is not available.
  • The current domain is not allowed to show the Consent Prompt.
note

The show method will automatically hide any other Consent Prompts that are currently displayed on the page.

Accessors

The following properties are available on the Consent Prompt JavaScript API:

shown

// Returns the path of the Consent Prompt that is currently shown in the form of <customerId>/<configId>.
readonly shown: string|undefined

The shown property returns the path of the Consent Prompt that is currently displayed on the page. If no Consent Prompt is currently displayed, this property will return undefined.