Custom Functions
How to make custom functions of the banner?
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 |
| None | Forces the Cookie Banner to appear. |
| None | Forces the Cookie Banner to close |
| 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']. |
| None | Returns an array of the available cookie categories as an array of the following cookie types: ['strict', 'performance', 'targeting', 'functionality', 'unclassified']. |
| None | Manually accepts all cookies. |
| None | Manually rejects all cookies except for the strictly necessary. |
| 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: [ strict
, performance
, targeting
, functionality
, unclassified
].
Name | Description |
| Means that the user has agreed to one or more cookie categories. List of accepted categories can be found in the |
| Means the user rejected all cookies. In this case |
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
How to stop showing CookieScript cookie banner?
If there is a problem that some website editors always show CookieScript cookie banner, you can stop showing it with this code. The code is also useful if your banner shows up in the iframe which loads under the same domain and you don't need to show the banner in that iframe.
<script type="text/javascript"
charset="UTF-8"
src="//cdn.cookie-script.com/s/xxxxxxxxxxxxxxxxxxxxxxxxxxxx.js"
data-cs-restrict-domain="true">
</script>
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?