Cookies are data which let you store user information in web pages. They are often used to track users’ data to re-use on their next visits to a web page. Creating and deleting cookies with jQuery is really easy thanks to the js-cookie library.
JavaScript cookie is a simple, lightweight JavaScript API for handling cookies. You can download it here. It can work in all browsers, accept any character. It has no dependency, supports ES modules and AMD/CommonJS with custom encoding/decoding.
Table of Contents
Init the plugin via CDN
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js.cookie.min.js"></script>
Create a cookie
Cookies.set('website', 'TL Templates');
//create a cookie which expires 15 days from now and works for all pages
Cookies.set('website', 'TL Templates', { expires: 15 });
//create a cookie which expires 15 days from now and works for certain page
Cookies.set('website', 'TL Templates', { expires: 15, path: '/test' });
Read cookie data
Get all cookies
Cookies.get();
Get specific cookie
Cookies.get('website'); //output: 'TL Templates'
Cookies.get('some_cookie'); //output: undefined
Delete a cookie
Cookies.remove('website');