Socket
Socket
Sign inDemoInstall

jaaulde-cookies

Package Overview
Dependencies
0
Maintainers
2
Versions
6
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    jaaulde-cookies

Javascript library for accessing and manipulating HTTP cookies in the web browser


Version published
Weekly downloads
74
increased by1.37%
Maintainers
2
Install size
22.5 kB
Created
Weekly downloads
 

Readme

Source

cookies

Javascript library for accessing and manipulating HTTP cookies in the web browser.

Get one or a list of cookies, set cookies, delete cookies, test if the browser accepts cookies. When JSON support is available, any JS value can be set to a cookie--it will be automatically serialized before being written, and un-serialzied on read.

GitHub version Bower version NPM version

installation

bower

bower install jaaulde-cookies

npm

npm install jaaulde-cookies

html

Download the code, link it in your HTML file.

<script src="/path/to/jaaulde-cookies.js"></script>

usage

This library is intended for use in the browser to access and manipulate cookies. It provides a singleton API, cookies.

As you'll see in the docs below, many of the methods can take an options parameter. The options that can be set are:

OptionDescriptionDefaultNote
domainDomain for which the cookie be availablenull (current domain)
pathPath for which the cookie be available'/'
expiresDate object representing expiration date/time of cookienull (expires when browser closes)Setting a past date/time will delete the cookie
secureShould cookie be sent to server via HTTPS only?false
cookies.test()
signature
/**
 * test - test whether the browser is accepting cookies
 *
 * @access public
 * @static
 * @return {boolean}
 */
test: function ()
example
if (cookies.test()) {
    // browser is accepting cookies!
}

Set cookies

cookies.set()
signature
/**
 * set - set or delete a cookie with desired options
 *
 * @access public
 * @static
 * @param {string} n - name of cookie to set
 * @param {mixed} v - Any JS value. If not a string and JSON support present will be JSON encoded
 *                  {null} to delete
 * @param {object} o - optional list of cookie options to specify
 * @return {void}
 */
set: function (n, v, o)
examples
// sets cookie by the name of 'myCookie' to value of 'myValue' with default options
cookies.set('myCookie', 'myValue');

// sets cookie by the name of 'myCookie' to value of 'myValue' with path of '/somedir'
cookies.set('myCookie', 'myValue', {path: '/somedir'});

Get cookies

cookies.get()
signature
/**
 * get - get one, several, or all cookies
 *
 * @access public
 * @static
 * @param {mixed} n {string} name of single cookie
 *                  {array} list of multiple cookie names
 *                  {void} if you want all cookies
 * @return {mixed} type/value of cookie as set
 *                 {null} if only one cookie is requested and is not found
 *                 {object} hash of multiple or all cookies (if multiple or all requested)
 */
get: function (n)
examples
// returns value of myCookie if it is present, null if not
var my_cookie = cookies.get('myCookie');

// returns object in key/value form of each requested cookie if it is present, null if not
var some_cookies = cookies.get(['myCookie', 'myOtherCookie']);

// returns object in key/value form of all available cookies from your site
var all_cookies = cookies.get();

Get filtered list of Cookies

cookies.filter()
signature
/**
 * filter - get hash of cookies whose names match the provided RegExp
 *
 * @access public
 * @static
 * @param {RegExp} p The regular expression pattern to match against cookie names
 * @return {object} hash of cookies whose names match the RegExp
 */
filter: function (p)
examples
// returns object in key/value form of cookies whose names start with "site"
var filtered_cookies = cookies.filter(/^site/);

Delete Cookies

note: A cookie can only be deleted using the same options with which it was set

cookies.del()
signature
/**
 * del - delete a cookie (domain and path options must match those with which the cookie was set; this is really an alias for set() with parameters simplified for this use)
 *
 * @access public
 * @static
 * @param {mixed} n {string} name of cookie to delete
 *                  {boolean} true to delete all
 * @param {object} o optional list of cookie options to specify (path, domain)
 * @return {void}
 */
del: function (n, o)
examples
// deletes a cookie, 'myCookie', with default options
cookies.del('myCookie');

// deletes a cookie by the name of 'myCookie' which had been set with a path of '/somedir'
cookies.del('myCookie', {path: '/somedir'});

// deletes all cookies
cookies.del(true);

Keywords

FAQs

Last updated on 01 Apr 2015

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc