A pure Javascript Cookie management controller Library.
Clone a copy of the main Cookmate git repo by running:
git clone git://github.com/jqrony/cookmate.git
In the cookmate/lib
folder you will find build version of cookmate along with the minified copy and associated map file.
# install locally (recomended)
npm install cookmate --save
Below are some of the most common ways to include Cookmate
<!--Including Cookmate (recomended) HTML document in head section -->
<script src="https://cdn.jsdelivr.net/npm/cookmate/lib/cookmate.min.js"></script>
new Cookmate.set(
String name, // ex: token, first-name etc. (required)
Mixed value, // Mixed ex: 4, foo, true etc. (required)
Timestamp expires, // 3600, Thu, 13 Jan 2024 (required)
String path, // Allow: /path (optional)
String domain, // Allow: example.com (optional)
Boolean secure, // Allow: true/false (optional)
Boolean HttpOnly, // Allow: true/false (optional)
String sameSite, // Allow: Strict, Lax, None (optional)
String priority // Allow: High, Medium, Low (optional)
);
/**
* @param {key} required
* @returns Boolean true/false
*/
new Cookmate.has(String key);
/**
* @param {key} required
* @returns matched value
*/
new Cookmate.get(String key);
/**
* @param {key} String (required)
* @param {path} "/path" (optional)
* @param {domain} "example.com" (optional)
*/
new Cookmate.remove(String key, String path, String domain);
There are several ways to use Webpack, Browserify or Babel. For more information on using these tools, please refer to the corresponding project's documentation. In the script, including Cookmate will usually look like this:
import cookmate from "cookmate";
If you need to use Cookmate in a file that's not an ECMAScript module, you can use the CommonJS syntax:
const cookmate = require("cookmate");
AMD is a module format built for the browser. For more information, we recommend
define(["cookmate"], function(cookmate) {
});
if include Cookmate library or CDN Link in document file. then you use pure javascript syntax:
const cookmate = new Cookmate();
cookmate.toJson();
// Output: {"id": 1, "user": "foo"}
cookmate.parse();
// Output: {id: 1, user: "foo"}
cookmate.serialize();
// Output: id=1&user=foo
cookmate.getAll();
// Output: {...} returns all cookie data
// Clearing all cookie on active location
cookmate.clear();
cookmate.set("id", 1, 3600, "/");
cookmate.set("user", "foo", "Thu, 13 Jan 2024", "/");
cookmate.remove("id");
// if in case change path and domain then
cookmate.remove("key", "/root", "example.com");
cookmate.get("user");
// Output: foo
cookmate.has("user");
// Output: true
fire cookmate change
event when cookie set, remove, clear
cookmate.on("change", fucntion(event) {
console.log(event.deleted, event.changed);
});