Custom Functions

In some cases, you might need to be able to control your Cookie Banner behavior programmatically or read the current state of the user consent. CookieScript provides a set of functions that can be utilized to customize cookie controls programmatically.

These functions are accessible via CookieScript.instance once it is initialized.

List of CookieScript functions and their description

Name Parameters Description
show None forces the Cookie Banner to appear.
hide None forces the Cookie Banner to close

currentState


None

returns the current state of the user's consent as an object with two properties:

action: 'accept' | 'reject' ;

categories: ['strict', 'performance', 'targeting', 'functionality', 'unclassified'].


categories


None

returns an array of the available cookie categories as an array of the following cookie types: 

['strict', 'performance', 'targeting', 'functionality', 'unclassified'].

acceptAllAction None manually accepts all cookies.
rejectAllAction None manually rejects all cookies except for the strictly necessary.
acceptAction string array categories

manually accepts some of the cookie categories passed as its parameter:

CookieScript.instance.acceptAction(['performance', 'targeting']).

Show / Hide Cookie Banner

CookieScript banner can be shown or hidden with the following functions:

CookieScript.instance.show()


CookieScript.instance.hide()


This would be equivalent to making the banner to popup by clicking on the cookie badge or closing the banner by clicking on the close button.

Get the current consent state

The current cookie choice of the user can be read by calling this function: 

CookieScript.instance.currentState()


The function returns an object with 2 properties:

{

action:'accept'|'reject',

categories:['strict', 'performance', 'targeting', 'functionality', 'unclassified']

}


The action property can be either accept  or reject .

The categories property is an array of what the user has agreed to and may contain any of the following categories: [ strictperformancetargetingfunctionalityunclassified ]


Name Description
accept means that the user has agreed to one or more cookie categories. List of accepted categories can be found in the  category  property.
reject means the user rejected all cookies. In this case  categories  property still stores  strict  cookie category since it cannot be rejected.

Get available categories

Some websites do not require nor do they use all the cookie categories mentioned above. The cookie types that are currently available for the user to choose from can be seen using the following function:

CookieScript.instance.categories()


The function returns an array of available category keys:


let categories = CookieScript.instance.categories()

// the variable categories is going to look

// somewhat like this:

// ['performance', 'targeting', 'functionality']



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 cookie categories are enabled, they can be accepted programmatically using this function:

CookieScript.instance.acceptAction(categories)


The function requires a parameter with a list of the categories to be accepted, like in this example:

var categories = ['performance', 'targeting', 'functionality', 'unclassified'] CookieScript.instance.acceptAction(categories)


Code examples

A link that makes the Cookie Banner to appear:

<a href="javascript:CookieScript.instance.show()">show popup</a>


This is equivalent to clicking the CookieScript badge.

A link that lets the user to accept all cookies:

<a href="javascript:CookieScript.instance.acceptAllAction()">By continuing you are

accepting all cookies.</a>


A link that lets the user to accept some categories

<a href="javascript:CookieScript.instance.acceptAction(['performance',

'targeting'])">Accept</a>


CookieScript events

CookieScript also provides a variety of events to listen to Cookie Banner's actions. We have created a detailed explanation of how custom events work.

Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.