幫助你簡化Cookie操作的javascript…
cookie.js–simplifying cookies in JavaScript
cookie.js is a tiny JavaScript library that simplifies cookies. It is capable of setting, getting and removing cookies, accepts a variety of parameters, and supports chaining. cookie.js is released under the MIT/X11 license. It doesn't have any dependencies and if minified it's only ~1.4 KiB large (~0.7 KiB if minified and gzipped).
Why would you want to use it?
Working with cookies in JavaScript sucks.
document.cookie
is definitely one of the ugly parts of
JavaScript. This library aims to provide an easy and nevertheless
powerful way to use cookies.
Usage
Download cookie.min.js and include it in your HTML document:
If you include cookie.js as above, this script will add an
object called cookie
cookie.js also supports AMD. So if you want to include cookie.js dynamically, you can just require it with any AMD loader, for example RequireJS. Follow the instructions of your AMD loader to include cookie.js.
After that you can call any of methods that are explained in the following.
cookie.set()
You can use the cookie.set
method to set cookies.
The value will automatically be escaped for you.
You can also set several values at once:
If you need more options, like setting the expiry date, you can add an object with options as the third parameter:
The following fields can be added to the mentioned object:
key | value | default value |
---|---|---|
expires |
Either a number containing the days
until the expiry, a date in the GMTString format or a
date object . |
Expires when the browser is closed. |
domain |
A string that specifies the domain
that can access the cookie. |
The current domain. |
path |
A string that limits the access of
the cookie to that path. |
The current path. |
secure |
A boolean indicating whether the
cookie shall only be accessable over a secure connection or
not. |
false |
cookie.get()
This method allows you to retrieve your cookies, you can use it by simply passing the key of the cookie:
Passing just one key like this will return a string, containing the value of the cookie. You can also pass an array of keys:
This will always return an object. The keys of this object will be the keys you passed and the values are the corresponding values.
In case you want to add a default value you can use the second parameter. The default value will be returned if the cookie(s) could not be found:
This also works with several keys:
cookie()
is a shortcut for
cookie.get()
.
cookie.all()
To get all of the currently saved cookies simply call
cookie.all
. In this case the variable
cookies
will return an object with all the current
cookies.
cookie.remove()
This method allows you to remove cookies. It accepts an infinite number of keys or an array of keys.
cookie.empty()
Sometimes you may want to remove all cookies. Simply call
cookie.empty()
and every cookie will be removed.
cookie.enabled()
This method allows you to test if the cookies are enabled. It
returns true
if you can work with cookies and
false
if you cannot. You might want to use a fallback
if they are disabled:
Chaining
The methods set
, remove
and
empty
return the cookie object and therefore enable
chaining.
Contribute
If you find any bugs in this script or have ideas how to improve it please let me know. You are also very welcome to contribute to the code here on github.