npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

jaaulde-cookies

v3.0.6

Published

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

Downloads

230

Readme

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.

Cookie options

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

|Option|Description|Default|Note| |:-----|:----------|:------|:---| |domain|Domain for which the cookie be available|null (current domain)|| |path|Path for which the cookie be available|'/'|| |expires|Date object representing expiration date/time of cookie| null (expires when browser closes)|Setting a past date/time will delete the cookie| |secure|Should cookie be sent to server via HTTPS only?|false||

Test for browser cookie acceptance

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