Accessing Consent in JavaScript (SDK)
Complete reference for the Ours Privacy CMP JavaScript SDK, including methods to read consent state, update consent, and control banner visibility.
Accessing Consent in JavaScript (SDK)
You can interact with the CMP on your site using the global window.ours_consent object. This object provides a limited set of methods to read and update user consent, as well as control the visibility of the consent banner or modal.
Note: For most users, you do not need to use these methods directly. The consent UI and banner handle all standard consent flows for you. These APIs are intended for advanced or custom integration scenarios only.
SDK Methods
The following methods are available on window.ours_consent:
1. getConsent()
Get the full consent object.
const consent = window.ours_consent.getConsent();
// consent = {
// type: 'all' | 'custom' | 'necessary',
// acceptedCategories: string[],
// rejectedCategories: string[],
// }2. acceptCategory(category)
Programmatically accept one or more categories. This method accepts a string or an array of strings representing the categories to accept.
Important: This method replaces the entire consent state. It accepts the categories you pass in and rejects all non-necessary categories (categories not in the list). Each call replaces the previous state, so you cannot call this method multiple times in a row to incrementally add categories.
// Accept a single category
window.ours_consent.acceptCategory('analytics');
// Accept multiple categories
window.ours_consent.acceptCategory(['analytics', 'marketing']);3. rejectCategory(category)
Programmatically reject one or more categories. This method accepts a string or an array of strings representing the categories to reject.
This method adds the specified categories to the rejected categories list without replacing the entire consent state.
// Reject a single category
window.ours_consent.rejectCategory('analytics');
// Reject multiple categories
window.ours_consent.rejectCategory(['analytics', 'marketing']);4. acceptAll()
Accept all cookie categories at once. This is a convenience method that accepts all available categories in a single call.
window.ours_consent.acceptAll();
// All categories are now accepted5. necessaryOnly()
Accept only necessary (readonly) cookie categories, rejecting all others. This is a convenience method that sets consent to only the necessary categories.
window.ours_consent.necessaryOnly();
// Only necessary categories are accepted, all others are rejected6. getAcceptedCategories()
Get a list of all accepted categories.
const accepted = window.ours_consent.getAcceptedCategories();
// accepted = ['necessary', 'analytics']7. show()
Show the consent banner or modal programmatically.
window.ours_consent.show();8. showPreferences()
Show the preferences modal programmatically. Similar to show(), but opens the preferences interface instead of the main consent banner or modal.
window.ours_consent.showPreferences();9. hide()
Hide the consent banner or modal programmatically.
window.ours_consent.hide();10. on(event, callback)
Subscribe to consent-related events. This allows you to run custom code when consent changes or is first set.
- Supported events:
change: Fired when the user modifies their preferences and only if consent has already been provided.firstConsent: Fired only the very first time that the user expresses their choice of consent (accept/reject).consent: Fired the very first time the user expresses their choice of consent — just likefirstConsent— but also on every subsequent page load.close: Fired when the consent modal is dismissed via the close button.
Example:
window.ours_consent.on('change', (consent) => {
console.log('Consent changed:', consent);
});
window.ours_consent.on('firstConsent', (consent) => {
console.log('First consent set:', consent);
});
window.ours_consent.on('close', () => {
console.log('Consent modal was closed');
});Tip: Always check that
window.ours_consentis loaded before calling these methods. These APIs are intended for advanced integrations and most users will not need to use them directly.
Next Steps
- Consent Tracking: Learn about automatic consent event tracking
- Installation: Ensure the CMP script is properly installed
How is this guide?