Spruce comes with a few JS utilities needed for the UI components or in generic development.
The scripts utilize JS modules, so you have to use import to use them.
The scripts utilize JS modules, so you have to use import to use them. One thing that is good to know here is that you need to specify the type="module"
attribute and value for native browser support with import.
html<script type="module" src="your-script-with-import.js">
Cookie
A small utility to manage cookies from the client side (which is not straightforward in JS but sometimes needed).
setCookie
Set a cookie name-value pair with additional options.
Parameter(s)
Name | Default Value | Description |
---|---|---|
key | The name of the cookie. | |
value | The value of the cookie. | |
expires | null | Cookie expiration date in days. |
path | / | The URL path where the cookie exists. |
options | {}} | Any additional name and value pair. |
Any cookie which you set gets two default settings:
SameSite: 'Lax'
,Secure: true
.
If you want to overwrite any of them, use the last, options
parameter.
Example(s)
jsimport {setCookie} from '~sprucecss/js/cookie.js';
setCookie('spruce-test', true, 10, '/');
getCookie
Get a cookie value by name.
Parameter(s)
Name | Description |
---|---|
key | The name of the cookie. |
Example(s)
jsimport {getCookie} from '~sprucecss/js/cookie.js';
getCookie('spruce-test');
issetCookie
Check if a cookie exist by name.
Parameter(s)
Name | Description |
---|---|
key | The name of the cookie. |
Example(s)
jsimport {issetCookie} from '~sprucecss/js/cookie.js';
issetCookie('spruce-test');
removeCookie
Remove a cookie by name.
Parameter(s)
Name | Description |
---|---|
key | The name of the cookie. |
Example(s)
jsimport {removeCookie} from '~sprucecss/js/cookie.js';
removeCookie('spruce-test');