{"id":18055732,"url":"https://github.com/marinerer/cookie","last_synced_at":"2025-04-05T09:24:26.224Z","repository":{"id":93979139,"uuid":"73371346","full_name":"Marinerer/Cookie","owner":"Marinerer","description":"简单、轻量级的JavaScript cookie操作库📝","archived":false,"fork":false,"pushed_at":"2017-07-17T05:45:58.000Z","size":12,"stargazers_count":1,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-26T01:35:45.935Z","etag":null,"topics":["cookie","cookies","javascript-cookie"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Marinerer.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-11-10T10:26:16.000Z","updated_at":"2024-11-05T04:26:06.000Z","dependencies_parsed_at":"2023-04-20T10:33:21.461Z","dependency_job_id":null,"html_url":"https://github.com/Marinerer/Cookie","commit_stats":null,"previous_names":["marinerer/cookie","meqn/cookie"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Marinerer%2FCookie","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Marinerer%2FCookie/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Marinerer%2FCookie/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Marinerer%2FCookie/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Marinerer","download_url":"https://codeload.github.com/Marinerer/Cookie/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247313932,"owners_count":20918719,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cookie","cookies","javascript-cookie"],"created_at":"2024-10-31T01:11:43.612Z","updated_at":"2025-04-05T09:24:26.205Z","avatar_url":"https://github.com/Marinerer.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n# 关于 cookie.js\n\n[![GitHub issues](https://img.shields.io/github/issues/mengqing723/cookie.svg?style=flat-square)](https://github.com/mengqing723/cookie/issues)\n[![GitHub forks](https://img.shields.io/github/forks/mengqing723/cookie.svg?style=flat-square)](https://github.com/mengqing723/cookie/network)\n[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)](https://raw.githubusercontent.com/mengqing723/cookie/master/LICENSE)\n\n一个简单、轻量级的JavaScript cookie操作库\n\n\n# cookie 简介\n\ncookie 是存储于客户端中的变量。当用户访问网站后创建cookie文件，用于存储浏览信息，例如个人资料信息等。这些信息存放在客户端的计算机中，用于客户端计算机与服务器之间传递信息。  \n\n我们可以通过 document.cookie 来读取或设置这些信息。  \n\ncookie 是有大小限制的，每个 cookie 所存放的数据不能超过4kb，如果 cookie 字符串的长度超过4kb，则该属性将返回空字符串。  \n\ncookie 是存在 有效期的。在默认情况下，一个 cookie 的生命周期就是在浏览器关闭的时候结束。如果想要 cookie 能在浏览器关掉之后还可以使用，就必须要为该 cookie 设置有效期，也就是 cookie 的失效日期。 \n\n\n# Cookie 属性\n\n除名字与值外，每个cookie有四个可选属性：\n\nkey | value | default value\n---|---|---\n`expires` | 过期时间，即指定cookie的生命期。值为 某个时间。默认情况下cookie是暂时的，浏览器关闭就失效。如果想让cookie的存在期限超过当前浏览器会话时间，就必须使用这个属性。当过了到期日期时，浏览器就可以删除cookie文件，没有任何影响。 | 浏览器关闭过期\n`domain` | 有效域。值为 域名。如果把path设置为`\"/\"`，把domain设为\".baidu.com\"，那么 A.baidu.com和B.baidu.com 的所有网页都能够访问此cookie。 | 本域\n`path` | 路径。它指定了与cookie关联在一起的网页。默认是在和当前网页同一目录的网页中有效。如果把`path`设置为`\"/\"`，那么它对该网站的所有网页都可见了。 | `/`\n`secure` | 安全。指定cookie的值通过网络如何在用户和WEB服务器之间传递。这个属性的值或者是“`secure`”，或者为空。缺省情况下，该属性为空，也就是使用不安全的HTTP连接传递数据。如果一个 cookie 标记为`secure`，那么，它与WEB服务器之间就通过HTTPS或者其它安全协议传递数据。不过，设置了`secure`属性不代表其他人不能看到你机器本地保存的cookie。换句话说，把cookie设置为`secure`，只保证cookie与WEB服务器之间的数据传输过程加密，而保存在本地的cookie文件并不加密。如果想让本地cookie也加密，得自己加密数据。 | `false`\n\n\n# 如何使用\n\n```javascript\n/**\n * 两种写法效果一样\n * @name \t\tcookie 名称\n * @value \t\tcookie 值\n * @options \t设置 cookie 过期时间、路径、域、安全\n */\ncookie(name, value, options)\ncookie.set(name, value, options)\n\ncookie.set('name', 'john', {\n\texpires: 7,\n\tdomain: '',\n\tpath: '/',\n\tsecure: false\n})\n```\n\n\n### 设置 cookie\n\n**1. 设置 cookie 过期时间为 7天**\n\n```javascript\n// 写法1\ncookie('name', '小明', 7);\ncookie('name', '小明', {expires: 7});\n\n// 写法2\ncookie.set('name', '小明', 7);\ncookie.set('name', '小明', {expires: 7});\n```\n\n**2. 设置 cookie 过期时间为 凌晨00:00:00 过期**\n\n\u003e 时间格式一定为：  `hh:mm:ss`\n\n```javascript\ncookie('name', '小明', '23:59:59');\n\n// 或\n\ncookie.set('name', '小明', '23:59:59');\n```\n\n**3. 设置 cookie 过期时间为 2018/10/12 00:00:00 过期**\n\n\u003e 过期时间为 日期格式 : YYYY-MM-dd hh:mm:ss\n\n```javascript\ncookie('name', '小明', '2018/10/12');\n\n// 或\n\ncookie.set('name', '小明', '2018/10/12');\n```\n\n### 获取 cookie\n\n```javascript\ncookie()\n// 或\ncookie.all()\n// 输出所有 cookie\n\n\ncookie('name')\n// 或\ncookie.get('name')\n// 都输出 '小明'\n```\n\n\n### 删除 cookie\n\n```javascript\ncookie.clear() \t\t// 清空cookie\n\ncookie('name', null) \t// 删除cookie 'name'\ncookie.remove('name') \t// 删除cookie 'name'\n\ncookie.remove(['name', 'age']) \t\t// 删除cookie  'name','age'\n```\n\n\n\n\n\n---\n\n### 感谢\n\n由于每次写cookie 都要去找下代码，然后在修改，所以就参考了一些cookie，写了适合自己的cookie库。\n\n其他库\n[florian](https://github.com/florian/cookie.js) 、 [js-cookie](https://github.com/js-cookie/js-cookie) 、 [jaywcjlove](https://github.com/jaywcjlove/cookie.js) 、 [ScottHamper](https://github.com/ScottHamper/Cookies)\n\n\n# Bug tracker\n\nHave a bug? Please create an issue on GitHub at [issues](https://github.com/mengqing723/cookie/issues)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarinerer%2Fcookie","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmarinerer%2Fcookie","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarinerer%2Fcookie/lists"}