Web CMP JavaScript Methods
This page provides a list of all public methods exposed by the Web CMP script.
Close Cookie Banner
| Method | OneTrust.Close() |
| Description | Dismisses the cookie banner and sets the default consent model. |
Allow All Consent
| Method | OneTrust.AllowAll() |
| Description | Enables consent for all categories/purposes, hosts, and vendors. |
Reject All Consent
| Method | OneTrust.RejectAll() |
| Description | Disables consent for all categories/purposes, hosts, and vendors, except those marked as strictly necessary. |
Show Preference Center
| Method | OneTrust.ToggleInfoDisplay() |
| Description | Displays the Preference Center UI. |
Show Cookie Banner (Single Page Application)
| Method | OneTrust.LoadBanner() |
| Description | Loads the cookie banner (if configured in geo rules) when the OptanonAlertBoxClosed cookie is removed. |
| Pre-requisite | Ensure that ‘Enable single page application support’ is turned on in your script’s publish pane. |
| Considerations | Remove the onetrust-consent-sdk element from the page. Note: Calling LoadBanner() will reintroduce this element, and duplicate instances may cause UI issues. |
Insert Script
| Method | OneTrust.InsertScript(url, selector, callback, options, groupId, async) |
| Description | Injects scripts or files dynamically once user consent is granted. |
| Parameters | url (string): Source URL for the script tag. selector (string): Specifies the container element for the script tag. Acceptable values include "head", "body", or a custom element ID (e.g., "{element_id}"). callback (string): (Optional) Callback function to execute after the script tag has been successfully inserted. options (function): (Optional) Defines custom behaviors to execute after the script has been inserted. See below for more information. groupId (string): Purpose/category ID. async (boolean): Indicates whether the script should be loaded asynchronously. When set to true, the script loads in parallel without blocking page rendering. |
options
options// set to true to delete all selector content before inserting script
options.deleteSelectorContent(boolean)
// set to true to show selector after inserting script
options.makeSelectorVisible(boolean)
// set the IDs of arbitrary elements to show after inserting script
options.makeElementsVisible(array[string])
// set the IDs of arbitrary elements to delete after inserting script
options.deleteElements(array[string])
Sample reference
OneTrust.InsertScript('https://www.w3schools.com/js/myScript1.js','body', null, null, 'C0004', true);
Insert HTML
| Method | OneTrust.InsertHTML(element, selector, callback, options, groupId) |
| Description | Injects HTML elements dynamically once user consent is granted. |
| Parameters | element (string): HTML element string to be inserted selector (string): ID of the HTML parent element where the new element will be inserted. Example: "parent_id". callback (string): (Optional) Callback function to execute after the HTML element has been successfully inserted. options (function): (Optional) Defines custom behaviors to execute after the HTML element has been inserted. See below for more information. groupId (string): Purpose/category ID. |
options
options// set to true to delete all selector content before inserting element
options.deleteSelectorContent(boolean)
// set to true to show selector after inserting element
options.makeSelectorVisible(boolean)
// set the IDs of arbitrary elements to show after inserting element
options.makeElementsVisible(array[string])
// set the IDs of arbitrary elements to delete after inserting element
options.deleteElements(array[string])
Sample reference
OneTrust.InsertHtml('<div>some div</div>', '#container', null, null, 'C0004');
Block Google Analytics
| Method | OneTrust.BlockGoogleAnalytics(gaId, groupId) |
| Description | Block Google Analytics when consent has not been given. Sets window['ga-disable-${gaID}'] |
| Parameters | gaId (string): Google Analytics ID.groupId (string): Purpose/category ID. |
Trigger Google Analytics Event
| Method | OneTrust.TriggerGoogleAnalyticsEvent(category, action, label, value) |
| Description | Used to trigger Google Analytics event. We can by inspecting DataLayer event trackOptanonEvent. |
| Parameters | category (string): category action (string): action label (string): label value (string): value |
Example
OneTrust.TriggerGoogleAnalyticsEvent(category, action, label, value);
Is OptanonAlertBoxClosed Cookie Set
| Method | IsAlertBoxClosed() |
| Description | When true, OptanonAlertBoxClosed cookie is set. This will result in the Banner not being shown to the user, unless re-consent is flagged on initialization. |
| Parameters | n/a |
| Returns | boolean |
Example
OneTrust.IsAlertBoxClosed();
Is OptanonAlertBoxClosed and Valid
| Method | IsAlertBoxClosedAndValid() |
| Description | When true, OptanonAlertBoxClosed cookie is set. This will result in the Banner not being shown to the user, unless re-consent is flagged on initialization. |
| Parameters | n/a |
| Returns | boolean |
Example
OneTrust.IsAlertBoxClosedAndValid();
Set OptanonAlertBoxClosed Cookie
| Method | SetAlertBoxClosed() |
| Description | Used to set OptanonAlertBoxClosed cookie based on isOptanonAlertBoxCookiePersistent. |
| Parameters | n/a |
| Returns | n/a |
Example
OneTrust.SetAlertBoxClosed();
On Consent Changed Callback
| Method | OnConsentChanged(callback) |
| Description | Callback for consent changed event. |
| Parameters | callback (function): callback |
| Returns | n/a |
Example
OneTrust.OnConsentChanged(function() {});
Get Domain Data
| Method | GetDomainData() |
| Description | Get information about the cookie script - data includes, consent models, cookies in preference centre, etc. |
| Parameters | n/a |
| Returns | object |
Example
OneTrust.GetDomainData();
Returns
Contact OneTrust
Get Vendor Consent Requests
| Method | getVendorConsentsRequestV2() |
| Description | Will return getInAppTCData to support mobile applications. See more information IAB TCF 2.0 SDK Developer Guide. |
| Parameters | n/a |
| Returns | object |
Example
OneTrust.getVendorConsentsRequestV2();
Returns
Contact OneTrust
Test Log
| Method | testLog() |
| Description | View the configured script details and verify implementation using a new Javascript method in the browser console. |
| Parameters | n/a |
| Returns | console log |
Example
OneTrust.testLog();
Returns
- Domain with which the script is associated
- Consent model for the region
- If banner is shown or not shown
- Whether consent has been given (OptanonAlertBoxClosed presence)
- Current Category Statuses
- General Vendor Ids
- Geolocation and Template name
- List of active cookies with associated category IDs, host IDs, and vendor IDs

Change Language
| Method | changeLanguage(lang) |
| Description | Programmatically change the language that the banner and preference center are presented in on the page. This will throw an exception if the language has not been configured for the displayed template. |
| Parameters | lang(string): 2-digit ISO 639 language code. Make sure to configure the language for the Template and published. |
| Returns | n/a |
Example
OneTrust.changeLanguage("en"); // English
OneTrust.changeLanguage("fr"); // French
OneTrust.changeLanguage("de"); // German
OneTrust.changeLanguage("ja"); // Japansese
OneTrust.changeLanguage("pt"); // Portuguese
Fetch and Download Preference Center
| Method | FetchAndDownloadPC() |
| Description | Fetch the Preference Center JSON and render it to view. |
| Parameters | n/a |
| Returns | n/a |
Example
OneTrust.FetchAndDownloadPC();
Get Geolocation Data
| Method | getGeolocationData() |
| Description | Returns the location of the user as determined by the CloudFlare IP service |
| Parameters | n/a |
| Returns | JSON |
Get CSS
| Method | getCSS() |
| Description | Returns the CSS for the CMP. |
| Parameters | n/a |
| Returns | string |
Example
OneTrust.getCSS();
Returns
#onetrust-banner-sdk{ ... }
Update Consent
| Method | UpdateConsent(groupIdType,bitValue) |
| Description | Enable or disable consent for a category, host, or general vendor by providing the respective bit value |
| Parameters | groupIdType(string),bitValue(string) |
| Returns | n/a |
Example
OneTrust.UpdateConsent("Category","C0003:1"); //Enables consent for cookie category 3
OneTrust.UpdateConsent("Category","C0003:0"); //Disable consent for cookie category 3
OneTrust.UpdateConsent("Category","C0001:1,C0002:1,C0003:0"); //Enable consent for cookie category 1 and 2, Disable consent for cookie category 3
OneTrust.UpdateConsent("Host","H2:1"); //Enable consent for Host H2
OneTrust.UpdateConsent("Host","H2:0"); //Disable consent for Host H2
OneTrust.UpdateConsent("General Vendor","V33:1"); //Enable consent for General Vendor V33
OneTrust.UpdateConsent("General Vendor","V33:0"); //Disable consent for General Vendor V33
Set Data Subject Id
| Method | setDataSubjectId(dataSubjectId,isAnonymous, identifierType) |
| Description | Set an identifier for the current user. Optionally provide an identifier type or let it default to the default identifier set in Geolocation rules. More information on identifying users and setting default identifiers in Unified profile. |
| Parameters | dataSubjectId (string), isAnonymous (boolean = false), identifierType (string = Default Identifier) |
| Returns | n/a |
OneTrust.setDataSubjectId("someId") //sets Id to "someId", defaults to isAnonymous = false and identifierType = "Cookie Unique Id" or any default identifier set in geolocation rules
OneTrust.setDataSubjectId("someId", true) //sets Id to "someId", sets isAnonymous to true and defaults to identifierType = "Cookie Unique Id"
OneTrust.setDataSubjectId("[email protected]", false,"Email") // sets Id to "someId", sets isAnonymous to false and sets identifierType to "Email"
Sync Consent Profile
| Method | syncConsentProfile(dataSubjectId,JWT_Token,isAnonymous) |
| Description | Retrieves or logs consent for a user depending on the toggle status of 'Override server consent'. See more information here. When consent is retrieved from the server, you can see the payload returned in the Network calls via the "preferences" call. This method will update the value of OptanonConsent. |
| Parameters | dataSubjectId (string), JWT_Token (string) isAnonymous (boolean = false) |
| Returns | n/a |
Example
OneTrust.syncConsentProfile("Identifier","JWT_Token",false)
Set Consent Profile
| Method | setConsentProfile(payload) |
| Description | Update the consent values of OptanonConsent |
| Parameters | payload (string) - see example below |
| Returns | n/a |
Example
OneTrust.setConsentProfile({"purposes":
[{"Id":"PURPOSE_ID","TransactionType":"CONFIRMED"},
{"Id":"PURPOSE_ID","TransactionType":"NOT_GIVEN"},
{"Id":"PURPOSE_ID","TransactionType":"CONFIRMED"}],
"identifier":"dataSubjectId"});
Send Consent Receipt
| Method | SendReceipt |
| Description | Send a receipt immediately. The receipt will be backdated to the last interaction date. This is particularly useful when not using Cross-Domain and Cross-Device Consent but still wanting to log a receipt immediately after identifying a data subject, creating, or updating their profile. |
| Parameters | n/a |
| Returns | object |
Example
OneTrust.SendReceipt()
For more information, see Unified Profile.
Get Data Subject Id
| Method | getDataSubjectId() |
| Description | Retrieves the data subject ID |
Updated 1 day ago
