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

zebra_cookie

v3.0.0

Published

A ridiculously small (~500 bytes minified) JavaScript API for writing, reading and deleting browser cookies

Downloads

103

Readme

Zebra Cookie  Tweet

A ridiculously small (~500 bytes minified) JavaScript API for writing, reading and deleting browser cookies

npm Total Monthly License

Features

  • very easy to write, read and delete cookies
  • simple and intuitive syntax
  • extremely small: with around 500 bytes when minified (350 bytes gzipped), it is probably the smallest and most optimized API for handling browser cookies

🎂 Support the development of this project

Your support means a lot and it keeps me motivated to keep working on open source projects. If you like this project please ⭐ it by clicking on the star button at the top of the page. If you are feeling generous, you can buy me a coffee by donating through PayPal, or you can become a sponsor. Either way - Thank you! 🎉

Donate

Demo

See the demos

Installation

Zebra Cookie is available as a npm package. To install it use:

# the "--save" argument adds the plugin as a dependency in packages.json
npm install zebra_cookie --save

How to use

Load Zebra Cookie:

<script src="path/to/zebra_cookie.min.js"></script>

Alternatively, you can load Zebra Cookie from JSDelivr CDN like this:

<!-- for the most recent version, not recommended in production -->
<script src="https://cdn.jsdelivr.net/npm/zebra_cookie@latest/dist/zebra_cookie.min.js"></script>

<!-- for a specific version -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/zebra_cookie.min.js"></script>

<!-- replacing "min" with "src" will serve you the non-compressed version -->

Usage

// at this point "Cookie" object will be available in the global namespace
// the object exposes 3 methods that you can use to write, read and delete cookies

// create a session cookie (expires when the browser closes)
Cookie.write('cookie_name', 'cookie_value');

// create a cookie that expires in 1 day
Cookie.write('cookie_name', 'cookie_value', 24 * 60 * 60);

// read a cookie’s value
// following the examples above, this should return "cookie_value"
Cookie.read('cookie_name');

// the "read" method returns null if the cookie doesn’t exist
Cookie.read('non_existing_cookie_name');

// delete the cookie
Cookie.destroy('cookie_name');

Methods

destroy(name)

Removes a cookie from the browser.

Returns TRUE on success or FALSE otherwise.

Arguments

name - the name of the cookie to remove

// create a session cookie (expires when the browser closes)
Cookie.write('cookie_name', 'cookie_value');

// delete the cookie
Cookie.destroy('cookie_name');

read(name)

Reads the value of a cookie.

Returns the value of the requested cookie or null if the cookie doesn't exist.

Arguments

name - the name of the cookie to read

// create a session cookie (expires when the browser closes)
Cookie.write('cookie_name', 'cookie_value');

// read a cookie's value
// following the examples above, this should return "cookie_value"
Cookie.read('cookie_name');

write(name, value, [expire = 0], [path = /], [domain = ""], [secure = FALSE])

Sets a cookie in the browser.

Returns TRUE if the cookie was successfully set, or FALSE otherwise.

Arguments

name - The name of the cookie to set

value - The value to set

expire - (optional) - The life time of the cookie, in seconds. If set to 0, or omitted, the cookie will expire at the end of the session (when the browser closes).

path - (optional) - The path on the server in which the cookie will be available on. If set to /, the cookie will be available within the entire domain. If set to /foo/, the cookie will only be available within the /foo/ directory and all subdirectories such as /foo/bar/ of the domain. If omitted, it will be set to /.

domain - (optional) - The domain that the cookie will be available on. To make the cookie available on all sub-domains of example.com, domain should be set to to .example.com. The . (dot) is not required but makes it compatible with more browsers. Setting it to www.example.com will make the cookie available only in the www sub-domain.

secure - (optional) - Indicates whether cookie information should only be transmitted over a HTTPS connection. Default is FALSE.

// create a session cookie (expires when the browser closes)
Cookie.write('cookie_name', 'cookie_value');

// create a cookie that expires in 1 day
Cookie.write('cookie_name', 'cookie_value', 24 * 60 * 60);