even though way overdone i wanted to make my own package to manage cookies in html
use npm
$ npm i @m4rch/cookie
or my own cdn
<script src="https://cdn.m4rch.xyz/cookie.js"></script>
if you want to include it into jquery you can either use a cdn for both
<script src="https://cdn.m4rch.xyz/jquery.js"></script>
or
<script src="https://cdn.m4rch.xyz/cookie-jq.js"></script>
if you want to include jquery via another cdn
if you are using one of the jquery variants then you are going to have to use $.cookie
instead of cookie
cookie.set("name", "value")
values automatically get encoded and decoded using encodeURIComponent and decodeURIComponent, respectively
JSON objects and arrays are automatically stringified with JSON.stringify()
if they are passed as a value, null and undefined do not get assigned
options that can be passed are path and/or expires (in days)
cookie.set("name", "value", { path: "/", expires: 365 })
the default for path is /
and for expires is ""
, resulting in a session cookie
for the value of a specific cookie use
cookie.get("name")
values get automatically decoded using decodeURIComponent
JSON objects and arrays are automatically parsed by JSON.parse()
, if you don't want that use
cookie.get("name", { nojson: true })
for an array
of every key-value pair use
cookie.get()
or if you want an object
of every cookie use
cookie.get({ json: true })
cookie.remove("name")
if you want to specify a specific path use
cookie.remove("name", { path: "" })
the default for path is /