JavaScript Cookie
===
[![Buy me a coffee](https://img.shields.io/badge/Buy%20me%20a%20coffee-048754?logo=buymeacoffee)](https://jaywcjlove.github.io/#/sponsor)
[![Downloads](https://img.shields.io/npm/dm/cookiejs.svg?style=flat)](https://www.npmjs.com/package/cookiejs)
[![Build & Test](https://github.com/jaywcjlove/cookie.js/actions/workflows/ci.yml/badge.svg)](https://github.com/jaywcjlove/cookie.js/actions/workflows/ci.yml)
[![Coverage Status](https://jaywcjlove.github.io/cookie.js/badges.svg)](https://jaywcjlove.github.io/cookie.js/lcov-report/)
[![README-zh.md](https://jaywcjlove.github.io/sb/lang/chinese.svg)](./README-zh.md)
:cookie: A simple, lightweight JavaScript API for handling browser cookies, it is easy to pick up and use, has a reasonable footprint (~2kb) (gzipped: 0.84kb), and has no dependencies. It should not interfere with any JavaScript libraries or frameworks.
> Old [v1](https://raw.githack.com/jaywcjlove/cookie.js/v1-doc/index.html) version [document preview](https://raw.githack.com/jaywcjlove/cookie.js/v1-doc/index.html).
**Features:**
馃殌 Has no dependencies
馃尡 Works in all browsers
馃崄 Support TypeScript, including [d.ts](https://github.com/jaywcjlove/store.js/tree/master/types/index.d.ts) definition
馃敟 Heavily tested
馃摝 Supports AMD/CommonJS
馃挜 [cookie.min.js](https://github.com/jaywcjlove/cookie.js/tree/master/dist/cookie.min.js) 2.01kb(gzipped: 0.84kb)
## Usage
Installed via npm. You will need Node.js installed on your system.
```bash
$ npm install cookiejs --save
```
```js
import cookie from 'cookiejs';
cookie("test", "tank", 1)
```
Or manually download and link `cookiejs` in your HTML, It can also be downloaded via [UNPKG](https://unpkg.com/cookiejs/dist/) or [jsDelivr CDN](https://www.jsdelivr.com/package/npm/cookiejs):
```html
<script src="https://unpkg.com/cookiejs/dist/cookie.min.js"></script>
<script type="text/javascript">
cookie("test", "tank", 1);
</script>
```
## Basic Usage
> cookie(key, value, num)
> `key` cookie name
> `value` cookie value
> `num` expires time
```js
cookie('test', 'tank', 1) // Create a cookie that expires 1 days from now
cookie('test') // Create a cookie, valid across the entire site
cookie('test', null) // Delete cookie `test`
cookie() // Get all cookie
cookie.set('test', 'tank', 1) // ====cookie('test', 'tank', 1)
cookie.get('test') // ====cookie('test')
cookie.remove('test') // ====cookie('test',null)
cookie.remove('test3', 'test4') // Delete cookie `test3` and `test4`
cookie.clear() // Clean all cookie
cookie.all() // Get all cookie
```
## Set Cookie
`cookie.set(name, value, options)`
The same effect `cookie(name, value, options)`
Set the value of the cookie in batches
```js
cookie.set({
name1: 'value1',
name2: 'value2'
});
```
Create cookie that expires 30 days from now
```js
cookie('test', 'tank', 30); // Create a cookie that expires 30 days from now
cookie({ 'test':'123', 'test2':'456' }, { // 鎵归噺璁剧疆
'expires': 30,
'path': '/',
'domain':''
});
```
Create cookie that expires 30 days from now锛宎nd set cookie attributes
```js
cookie('test', '123', { 'expires': 30, 'path': '/', 'domain':'' });
```
## Cookie Attributes
individually for each call to `cookie.set(...)` by passing a plain object in the last argument. Per-call attributes override the default attributes.
**Examples:**
```js
cookie('name', 'value', { 'expires': 30, 'path': '/', 'domain':'' });
cookie.get('name')
cookie.remove('name')
```
### expires
Define when the cookie will be removed. Value can be a Number which will be interpreted as days from time of creation or a Date instance. If omitted, the cookie becomes a session cookie.
```js
cookie('name', 'value', { 'expires': 30 });
```
### path
> Default: `/`
A String indicating the path where the cookie is visible.
```js
cookie.set('name', 'value', { path: '' });
cookie.get('name'); // => 'value'
```
### domain
> Default: Cookie is visible only to the domain or subdomain of the page where the cookie was created, except for Internet Explorer ([see: Note regarding Internet Explorer default behavior](https://blogs.msdn.microsoft.com/ieinternals/2009/08/20/internet-explorer-cookie-internals-faq/)).
> 鈿狅笍If you omit the domain attribute, it will be visible for a subdomain in IE.
A String indicating a valid domain where the cookie should be visible. The cookie will also be visible to all subdomains.
Examples:
```js
cookie.set('name', 'value', { domain: 'subdomain.website.com' });
cookie.get('name'); // => undefined (need to read at 'subdomain.website.com')
```
### secure
> Default: No secure protocol requirement.
Either true or false, indicating if the cookie transmission requires a secure protocol (https).
Here's an examples:
```js
cookie.set('name', 'value', { secure: true });
cookie.get('name'); // => 'value'
cookie.remove('name');
```
### SameSite
The [SameSite](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie/SameSite) attribute lets servers specify whether/when cookies are sent with cross-site requests (where [Site](https://developer.mozilla.org/en-US/docs/Glossary/Site) is defined by the registrable domain and the scheme: http or https). This provides some protection against cross-site request forgery attacks ([CSRF](https://developer.mozilla.org/en-US/docs/Glossary/CSRF)). It takes three possible values: Strict, Lax, and None.
With Strict, the cookie is only sent to the site where it originated. Lax is similar, except that cookies are sent when the user navigates to the cookie's origin site. For example, by following a link from an external site. None specifies that cookies are sent on both originating and cross-site requests, but only in secure contexts (i.e., if `SameSite=None` then the Secure attribute must also be set). If no `SameSite` attribute is set, the cookie is treated as Lax.
Here's an example:
```js
cookie.set('name', 'value', { sameSite: 'Strict' });
```
> Note: The standard related to `SameSite` recently changed (MDN documents the new behavior above). See the cookies [Browser compatibility](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie/SameSite#browser_compatibility) table for information about how the attribute is handled in specific browser versions:
>
> `SameSite=Lax` is the new default if `SameSite` isn't specified. Previously, cookies were sent for all requests by default.
> - Cookies with `SameSite=None` must now also specify the `Secure` attribute (they require a secure context).
> - Cookies from the same domain are no longer considered to be from the same site if sent using a different scheme (`http:` or `https:`).
## Related
- [storejs](https://github.com/jaywcjlove/store.js) A simple, lightweight JavaScript API for handling browser localStorage , it is easy to pick up and use, has a reasonable footprint 2.36kb(gzipped: 1.04kb), and has no dependencies.
## Contributors
As always, thanks to our amazing contributors!
<a href="https://github.com/jaywcjlove/cookie.js/graphs/contributors">
<img src="https://jaywcjlove.github.io/cookie.js/CONTRIBUTORS.svg" />
</a>
Made with [action-contributors](https://github.com/jaywcjlove/github-action-contributors).
## License
Licensed under the MIT License.
没有合适的资源?快使用搜索试试~ 我知道了~
本地存储cookie的封装,提供简单的AIP.zip
共18个文件
js:6个
md:3个
yml:2个
需积分: 5 0 下载量 55 浏览量
2024-05-15
14:34:57
上传
评论
收藏 23KB ZIP 举报
温馨提示
JavaScript一种直译式脚本语言,是一种动态类型、弱类型、基于原型的语言,内置支持类型。它的解释器被称为JavaScript引擎,为浏览器的一部分,广泛用于客户端的脚本语言,最早是在HTML(标准通用标记语言下的一个应用)网页上使用,用来给HTML网页增加动态功能。 在1995年时,由Netscape公司的Brendan Eich,在网景导航者浏览器上首次设计实现而成。因为Netscape与Sun合作,Netscape管理层希望它外观看起来像Java,因此取名为JavaScript。但实际上它的语法风格与Self及Scheme较为接近。 为了取得技术优势,微软推出了JScript,CEnvi推出ScriptEase,与JavaScript同样可在浏览器上运行。为了统一规格,因为JavaScript兼容于ECMA标准,因此也称为ECMAScript。
资源推荐
资源详情
资源评论
收起资源包目录
本地存储cookie的封装,提供简单的AIP.zip (18个子文件)
新建文本文档.txt 2KB
cookie.js-master
rollup.config.mjs 2KB
.github
workflows
ci.yml 5KB
FUNDING.yml 406B
renovate.json 148B
src
main.js 3KB
index.d.ts 898B
docs
README-zh.md 15B
dist
cookie.cjs.js 4KB
cookie.esm.js 4KB
cookie.js 4KB
cookie.min.js 2KB
demo.html 980B
package.json 1KB
test
cookie.test.js 3KB
.gitignore 123B
README.md 7KB
README-zh.md 8KB
共 18 条
- 1
资源评论
野生的狒狒
- 粉丝: 2629
- 资源: 2167
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功