{"id":16772619,"url":"https://github.com/jonlabelle/cookie-js","last_synced_at":"2025-04-10T19:52:32.586Z","repository":{"id":4301143,"uuid":"5432773","full_name":"jonlabelle/cookie-js","owner":"jonlabelle","description":"A tiny (1.24 KB gzipped), stand-alone JavaScript utility for managing cookies in the browser.","archived":false,"fork":false,"pushed_at":"2020-08-18T12:10:08.000Z","size":116,"stargazers_count":15,"open_issues_count":0,"forks_count":11,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-03-24T17:51:26.658Z","etag":null,"topics":["cookie","javascript","javascript-utility","vanilla-javascript"],"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/jonlabelle.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2012-08-15T23:15:45.000Z","updated_at":"2024-06-18T10:49:40.000Z","dependencies_parsed_at":"2022-08-06T16:00:52.709Z","dependency_job_id":null,"html_url":"https://github.com/jonlabelle/cookie-js","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonlabelle%2Fcookie-js","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonlabelle%2Fcookie-js/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonlabelle%2Fcookie-js/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonlabelle%2Fcookie-js/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jonlabelle","download_url":"https://codeload.github.com/jonlabelle/cookie-js/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248284081,"owners_count":21077990,"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","javascript","javascript-utility","vanilla-javascript"],"created_at":"2024-10-13T06:43:24.899Z","updated_at":"2025-04-10T19:52:32.566Z","avatar_url":"https://github.com/jonlabelle.png","language":"JavaScript","readme":"# Cookie.js\n\nA tiny (1.24 KB gzipped), stand-alone JavaScript utility for managing cookies in the browser.\n\n## Usage\n\nAdd [Cookie.min.js](https://raw.githubusercontent.com/jonlabelle/cookie-js/master/Cookie.min.js) to your HTML document.\n\n```html\n\u003cscript src=\"Cookie.min.js\"\u003e\u003c/script\u003e\n```\n\n## API\n\n### Cookie.set()\n\n**Create a cookie with no options specified**:\n\n```javascript\nCookie.set('name', 'jon');\n```\n\n\u003e **NOTE:** If the `option.expires` value is not set, the cookie `Expires / Max-Age` is set to *Session*.\n\n**Create a cookie with an expiration date:**\n\n```javascript\nCookie.set('name', 'jon', {\n  expires: new Date('March 18, 2040')\n});\n```\n\n**Create a cookie that expires in 3 days:**\n\n```javascript\nCookie.set('name', 'jon', {\n  expires: 3\n});\n```\n\n**Create a cookie that can only be accessed by a specific** `path` **and** `domain`**:**\n\n```javascript\nCookie.set('name', 'jon', {\n  path: '/', // all pages\n  domain: 'jonlabelle.com' // any subdomain of jonlabelle.com (including www)\n});\n```\n\n**Create a secure cookie:**\n\n```javascript\nCookie.set('name', 'jon', {\n  secure: true\n});\n```\n\n\u003e **NOTE:** Setting the `secure` option to `true` ensures the cookie will not be sent over non-https connections.\n\n**Create a cookie and set the** [SameSite](https://developer.mozilla.org/docs/Web/HTTP/Headers/Set-Cookie/SameSite) **attribute:**\n\n```javascript\nCookie.set('name', 'jon', {\n  sameSite: 'Strict'\n});\n```\n\nThe `SameSite` attribute accepts three values:\n\n|       Value       |                                                                            Description                                                                            |\n|-------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `Lax` (*default*) | Cookies can sent in top-level navigations and will be sent along with GET request initiated by third party website. This is the default value in modern browsers. |\n| `Strict`          | Cookies will only be sent in a first-party context and not be sent along with requests initiated by third party websites.                                         |\n| `None`            | Cookies will be sent in all contexts, i.e sending cross-origin is allowed.                                                                                        |\n\n\u003e **IMPORTANT:** If `SameSite` is not specified in cookie options, the default value will be set to `Lax` for a reasonably robust defense against some classes of cross-site request forgery. Learn more at [SameSite cookies explained](https://web.dev/samesite-cookies-explained).\n\n### Cookie.get()\n\n**Get a cookie accessible by the current page:**\n\n```javascript\nCookie.get('name');\n```\n\n\u003e **NOTE:** Returns `null` if the cookie does NOT exist.\n\n### Cookie.exists()\n\n**Check if a cookie exists:**\n\n```javascript\nif (Cookie.exists('name')) {\n  // do cool stuff here\n}\n```\n\n\u003e **NOTE:** Returns `bool`, `true` if the cookie exists, and `false` if it does not.\n\n### Cookie Value Types\n\n**Retrieve a cookie and convert the value to** `Number`**:**\n\n```javascript\nCookie.set('age', 34);\n\nvar val = Cookie.get('age', Number);\n\nif (typeof val === 'number') {\n  console.log(val); // 34\n}\n```\n\nOther native functions that convert values are `Boolean` and `Date`, or you can define your own conversion `Function`.\n\nFor example, to create a number from a hexadecimal code:\n\n```javascript\nvar value = Cookie.get('code', function (stringValue) {\n  return parseInt(stringValue, 16);\n});\n```\n\n### Cookie.remove()\n\n**Delete a cookie:**\n\n```javascript\nCookie.remove('name');\n```\n\n**Delete a cookie specifying the** `domain`**:**\n\n```javascript\nCookie.remove('info', {\n  domain: 'jonlabelle.com'\n});\n```\n\n### Cookie.setSub()\n\nSub-cookies allow multiple values to be stored in a single cookie. A sub-cookie looks similar to a URL and takes the following form:\n\n```text\ncookiename=name1=value1\u0026name2=value2\u0026name3=value3\n```\n\n**Create a sub-cookie named** `person`**:**\n\n```javascript\nCookie.setSub('person', 'name', 'jon');\nCookie.setSub('person', 'email', 'contact@jonlabelle.com');\nCookie.setSub('person', 'today', (new Date()).toString());\n```\n\n**Create a sub-cookie with options:**\n\n```javascript\nCookie.setSub('person', 'age', 75, { domain: 'jonlabelle.com', secure: true });\n```\n\n**Create a sub-cookie from an** `Object`**:**\n\n```javascript\nvar obj = {\n  name: 'jon',\n  email: 'labelle'\n};\n\nCookie.setSubs('person', obj);\n```\n\n\u003e **NOTE:** Calls to `Cookie.setSubs()` will completely overwrite the cookie.\n\n### Cookie.getSub()\n\n**Get a sub-cookie:**\n\n```javascript\nCookie.getSub('person', 'name');\n```\n\n**Get a sub-cookie and convert the value to a** `Number`**:**\n\n```javascript\nCookie.getSub('person', 'age', Number);\n```\n\n### Cookie.getSubs()\n\n**Get a sub-cookie as a hash** `Object`**:**\n\n```javascript\nvar obj = Cookie.getSubs('person');\n\nif (typeof obj === 'object') {\n  console.log(obj); // =\u003e Object { name: 'jon', email: '...'}\n}\n```\n\n### Cookie.removeSub()\n\n**Remove a sub-cookie:**\n\n```javascript\nCookie.removeSub('person', 'name');\n```\n\n### Cookie.enabled()\n\n**Check if cookies are enabled by the browser:**\n\n```javascript\nCookie.enabled();\n```\n\n\u003e **NOTE:** Returns `bool`, `true` if cookies are enabled, and `false` if they are not.\n\n### Cookie.clear()\n\n**Clear all cookies from the browser:**\n\n```javascript\nCookie.clear();\n```\n\n## Author\n\n[Jon LaBelle](mailto:contact@jonlabelle.com)\n\n## License\n\n[MIT License](LICENSE.txt)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjonlabelle%2Fcookie-js","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjonlabelle%2Fcookie-js","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjonlabelle%2Fcookie-js/lists"}