All Projects → jonlabelle → Cookie Js

jonlabelle / Cookie Js

Licence: mit
A tiny (1.24 KB gzipped), stand-alone JavaScript utility for managing cookies in the browser.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Cookie Js

Sticky Js
Library for sticky elements written in vanilla javascript
Stars: ✭ 618 (+5050%)
Mutual labels:  vanilla-javascript
Mind Elixir Core
Mind-elixir is a framework agnostic mind map core
Stars: ✭ 798 (+6550%)
Mutual labels:  vanilla-javascript
Albinotonnina.com
source-code
Stars: ✭ 837 (+6875%)
Mutual labels:  vanilla-javascript
Inputmask
Input Mask plugin
Stars: ✭ 5,695 (+47358.33%)
Mutual labels:  vanilla-javascript
Vanilla Lazyload
LazyLoad is a lightweight, flexible script that speeds up your website by deferring the loading of your below-the-fold images, backgrounds, videos, iframes and scripts to when they will enter the viewport. Written in plain "vanilla" JavaScript, it leverages IntersectionObserver, supports responsive images and enables native lazy loading.
Stars: ✭ 6,596 (+54866.67%)
Mutual labels:  vanilla-javascript
Silhouette
Silhouette is a framework agnostic authentication library for Scala that supports several authentication methods, including OAuth2, OpenID Connect, Credentials, Basic Authentication or custom authentication schemes.
Stars: ✭ 18 (+50%)
Mutual labels:  cookie
Helpjs Ravi
Exercícios/Tutorial/Desafios para Iniciantes em JavaScript
Stars: ✭ 579 (+4725%)
Mutual labels:  vanilla-javascript
Simple Cookie Choices
A simple cookie choices thought to the GDPR rules 🔒🍪
Stars: ✭ 12 (+0%)
Mutual labels:  cookie
Splide
Splide is a lightweight, powerful and flexible slider and carousel, written in pure JavaScript without any dependencies.
Stars: ✭ 786 (+6450%)
Mutual labels:  vanilla-javascript
Cookie
HTTP server cookie parsing and serialization
Stars: ✭ 848 (+6966.67%)
Mutual labels:  cookie
Authelia
The Single Sign-On Multi-Factor portal for web apps
Stars: ✭ 11,094 (+92350%)
Mutual labels:  cookie
Emoji Button
Vanilla JavaScript emoji picker component
Stars: ✭ 646 (+5283.33%)
Mutual labels:  vanilla-javascript
Cookie Session
Simple cookie-based session middleware
Stars: ✭ 928 (+7633.33%)
Mutual labels:  cookie
Knife
A burp extension that add some useful function to Context Menu 添加一些右键菜单让burp用起来更顺畅
Stars: ✭ 626 (+5116.67%)
Mutual labels:  cookie
Infinite Carousel
A timed infinite carousel that uses vanilla JavaScript & CSS animations.
Stars: ✭ 9 (-25%)
Mutual labels:  vanilla-javascript
Pythonspidernotes
Python入门网络爬虫之精华版
Stars: ✭ 5,634 (+46850%)
Mutual labels:  cookie
Laravel Cookie Consent
Make your Laravel app comply with the crazy EU cookie law
Stars: ✭ 895 (+7358.33%)
Mutual labels:  cookie
Summary
个人总结 持续更新 欢迎提出各种issues
Stars: ✭ 12 (+0%)
Mutual labels:  cookie
Proxy Storage
Provides an adapter for storage mechanisms (cookies, localStorage, sessionStorage, memoryStorage) and implements the Web Storage interface
Stars: ✭ 10 (-16.67%)
Mutual labels:  cookie
Jquery Ezstorage
jQuery EZStorage Plugin: manages browser side storage of data
Stars: ✭ 7 (-41.67%)
Mutual labels:  cookie

Cookie.js

A tiny (1.24 KB gzipped), stand-alone JavaScript utility for managing cookies in the browser.

Usage

Add Cookie.min.js to your HTML document.

<script src="Cookie.min.js"></script>

API

Cookie.set()

Create a cookie (with no options):

Cookie.set('name', 'jon');

NOTE: If the option.expires value is not set, the cookie Expires / Max-Age is set to Session.

Create a cookie with an expiration date:

Cookie.set('name', 'jon', {
  expires: new Date('March 18, 2040')
});

Create a cookie that expires in 3 days:

Cookie.set('name', 'jon', {
  expires: 3
});

Create a cookie that can only be accessed by a specific path and domain:

Cookie.set('name', 'jon', {
  path: '/', // all pages
  domain: 'jonlabelle.com' // any subdomain of jonlabelle.com (including www)
});

Create a secure cookie:

Cookie.set('name', 'jon', {
  secure: true
});

NOTE: Setting the secure option to true ensures the cookie is always encrypted when transmitting from client to server.

Cookie.get()

Get a cookie accessible by the current page:

Cookie.get('name');

NOTE: Returns null if the cookie does NOT exist.

Cookie.exists()

Check if a cookie exists:

if (Cookie.exists('name')) {
  // do cool stuff here
}

Returns bool, true if the cookie exists, and false if it does not.

Cookie Value Types

Retrieve a cookie and convert the value to Number:

Cookie.set('age', 34);

var val = Cookie.get('age', Number);

if (typeof val === 'number') {
  console.log(val); // 34
}

Other native functions that convert values are Boolean and Date, or you can define your own conversion Function.

For example, to create a number from a hexadecimal code:

var value = Cookie.get('code', function (stringValue) {
  return parseInt(stringValue, 16);
});

Cookie.remove()

Delete a cookie:

Cookie.remove('name');

Delete a cookie specifying the domain:

Cookie.remove('info', {
  domain: 'jonlabelle.com'
});

Cookie.setSub()

Sub-cookies allow multiple values to be stored in a single cookie. A sub-cookie looks similar to a URL and takes the following form:

cookiename=name1=value1&name2=value2&name3=value3

Create a sub-cookie named person:

Cookie.setSub('person', 'name', 'jon');
Cookie.setSub('person', 'email', '[email protected]');
Cookie.setSub('person', 'today', (new Date()).toString());

Create a sub-cookie with options:

Cookie.setSub('person', 'age', 75, { domain: 'jonlabelle.com', secure: true });

Create a sub-cookie from an Object:

var obj = {
  name: 'jon',
  email: 'labelle'
};

Cookie.setSubs('person', obj);

NOTE: Calls to Cookie.setSubs() will completely overwrite the cookie.

Cookie.getSub()

Get a sub-cookie:

Cookie.getSub('person', 'name');

Get a sub-cookie and convert the value to a Number:

Cookie.getSub('person', 'age', Number);

Cookie.getSubs()

Get a sub-cookie as a hash Object:

var obj = Cookie.getSubs('person');

if (typeof obj === 'object') {
  console.log(obj); // => Object { name: 'jon', email: '...'}
}

Cookie.removeSub()

Remove a sub-cookie:

Cookie.removeSub('person', 'name');

Cookie.enabled()

Check if cookies are enabled by the browser:

Cookie.enabled();

Returns bool, true if cookies are enabled, and false if they are not.

Cookie.clear()

Clears all cookies from the browser:

Cookie.clear();

Author

License

MIT License

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].