m4rch3n1ng / cookie

a small package to manage html cookies

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

@m4rch/cookie

even though way overdone i wanted to make my own package to manage cookies in html

Installation

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

documentation

if you are using one of the jquery variants then you are going to have to use $.cookie instead of cookie

.set(name, value, options?)

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

.get(name?, options?)

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 })

.remove(name, options?)

cookie.remove("name")

if you want to specify a specific path use

cookie.remove("name", { path: "" })

the default for path is /

About

a small package to manage html cookies

License:The Unlicense


Languages

Language:JavaScript 100.0%