Custom Functions
In some cases, you might need to be able to control cookie popup behavior programmatically or read the current state of the user consent. This is what Cookie-Script custom functions are designed for.
Get current user state
You can read the current user choice for cookie policy consent using this function of Cookie-Script instance:
CookieScript.instance.currentState()
The function returns an object with 2 properties:
{ action:'accept', categories:['strict', 'performance', 'targeting', 'functionality', 'unclassified'] }
action
: can be either accept
or reject
.
accept
means the user accepted to either one or many categories, list of accepted categories can be found in
category
property.
reject
means the user rejected all cookies. In this case
categories
property still stores
strict
cookie category since it cannot be rejected.
categories
: array with selected categories: [strict
, performance
, targeting
, functionality
, unclassified
]
Get available categories
Some websites do not use all the cookie categories. You can see what cookie categories are currently available for the user to choose from using the following function:
CookieScript.instance.currentState()
The function returns an array of available category keys:
categories = ['performance', 'targeting', 'functionality', 'unclassified']
Accept / Reject cookie policy
You can accept or reject the cookie policy with built-in functions. This might be handy if you want to place a link asking the user to accept cookies before some functionality will be available.
Accept all cookies
All cookies can be accepted with the following function:
CookieScript.instance.acceptAllAction()
This is the same as clicking Accept all button.
Reject all cookies
All cookies (except strictly necessary) can be rejected with this function:
CookieScript.instance.rejectAllAction()
This is the same as clicking Reject all button.
Accept some cookies
In case your popup has cookie categories, you can accept only some of them using this function:
CookieScript.instance.acceptAction(categories)
You should also pass a list of accepted categories as an array. Following categories are allowed:
var categories = ['performance', 'targeting', 'functionality', 'unclassified']
Show / Hide cookie banner
You can show or hide cookie popup with the following functions:
CookieScript.instance.show() CookieScript.instance.hide()
This is the same as closing a cookie popup with a close button or opening it by clicking on a cookie badge.
Cookie-Script events
Cookie-Script also provides a variety of events to listen to Cookie Consent popup actions. We have created a detailed explanation of how custom events work.
Code examples
A link that shows a popup
<a href="javascript:CookieScript.instance.show()">show popup</a>
A link that asks the user to accept all cookies
<a href="javascript:CookieScript.instance.acceptAllAction()">Please accept all cookies to continue</a>
Accept some categories
<a href="javascript:CookieScript.instance.acceptAction(['performance', 'targeting'])">Accept</a>