{"id":7957,"date":"2021-08-14T12:56:29","date_gmt":"2021-08-14T12:56:29","guid":{"rendered":"https:\/\/www.scmgalaxy.com\/tutorials\/?p=7957"},"modified":"2022-04-13T16:30:44","modified_gmt":"2022-04-13T16:30:44","slug":"replace-append-cookies-in-javascript","status":"publish","type":"post","link":"https:\/\/www.scmgalaxy.com\/tutorials\/replace-append-cookies-in-javascript\/","title":{"rendered":"Replace\/Append Cookies in JavaScript"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\"><strong>Replace\/Append Cookies<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">When we assign a new cookie value to document.cookie, the current cookie is not replaced. The new cookie is parsed and its name-value pair is appended to the list. The exception is when you assign a new cookie with the same name (and same domain and path, if they exist) as a cookie that already exists. In this case, the old value is replaced with the new.<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/vijaypk376\/446d251d1474e260e3353013eb9b8871.js\"><\/script>\n\n\n\n<script src=\"https:\/\/gist.github.com\/vijaypk376\/d6f9c779cd7e06d9db6bacafcefc4f7f.js\"><\/script>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Reading Cookie<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">We can read cookies by a document.cookie. The problem occurs when we need the specific part of the cookie to perform some action.<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/vijaypk376\/1cd4305d71987896e26f5a77beaabb73.js\"><\/script>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Deleting Cookies<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A cookie is deleted by setting a cookie with the same name (and domain and path, if they were set) with an expiration date in the past and if using max-age then must set a negative value.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Ex: <strong>&#8211;<\/strong><\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/vijaypk376\/41404c6b09198948b4505ae95d4b4572.js\"><\/script>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Updating Cookies<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A cookie is possible to update by setting a new value to a cookie with the same name.<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/vijaypk376\/d2c0fe84f70231bd257c4ae0eef4bac0.js\"><\/script>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Cookies Security Issues<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Can misuse Client Details<\/strong><\/li><li><strong>Can track User<\/strong><\/li><li><strong>Client Can Delete Cookies<\/strong><\/li><li><strong>Client can Manipulate Cookies<\/strong><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Cookies Limitation<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Support HTML4 \/ HTML 5<\/strong><\/li><li><strong>Each cookie can contain 4kb Data<\/strong><\/li><li><strong>Cookies can be stored in Browser and server<\/strong><\/li><li><strong>It is sent with each request<\/strong><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Replace\/Append Cookies When we assign a new cookie value to document.cookie, the current cookie is not replaced. The new cookie [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[16],"tags":[6318,5161,5095,423],"class_list":["post-7957","post","type-post","status-publish","format-standard","hentry","category-javascript","tag-append","tag-cookies","tag-javascript","tag-replace"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Replace\/Append Cookies in JavaScript - scmGalaxy<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.scmgalaxy.com\/tutorials\/replace-append-cookies-in-javascript\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Replace\/Append Cookies in JavaScript - scmGalaxy\" \/>\n<meta property=\"og:description\" content=\"Replace\/Append Cookies When we assign a new cookie value to document.cookie, the current cookie is not replaced. The new cookie [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.scmgalaxy.com\/tutorials\/replace-append-cookies-in-javascript\/\" \/>\n<meta property=\"og:site_name\" content=\"scmGalaxy\" \/>\n<meta property=\"article:published_time\" content=\"2021-08-14T12:56:29+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-04-13T16:30:44+00:00\" \/>\n<meta name=\"author\" content=\"rajeshkumar\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"rajeshkumar\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/replace-append-cookies-in-javascript\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/replace-append-cookies-in-javascript\\\/\"},\"author\":{\"name\":\"rajeshkumar\",\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/#\\\/schema\\\/person\\\/a40e0dd38feee7c2779460a804d49361\"},\"headline\":\"Replace\\\/Append Cookies in JavaScript\",\"datePublished\":\"2021-08-14T12:56:29+00:00\",\"dateModified\":\"2022-04-13T16:30:44+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/replace-append-cookies-in-javascript\\\/\"},\"wordCount\":206,\"commentCount\":0,\"keywords\":[\"Append\",\"Cookies\",\"Javascript\",\"Replace\"],\"articleSection\":[\"JavaScript\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/replace-append-cookies-in-javascript\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/replace-append-cookies-in-javascript\\\/\",\"url\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/replace-append-cookies-in-javascript\\\/\",\"name\":\"Replace\\\/Append Cookies in JavaScript - scmGalaxy\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/#website\"},\"datePublished\":\"2021-08-14T12:56:29+00:00\",\"dateModified\":\"2022-04-13T16:30:44+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/#\\\/schema\\\/person\\\/a40e0dd38feee7c2779460a804d49361\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/replace-append-cookies-in-javascript\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/replace-append-cookies-in-javascript\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/replace-append-cookies-in-javascript\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Replace\\\/Append Cookies in JavaScript\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/#website\",\"url\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/\",\"name\":\"scmGalaxy\",\"description\":\"A Platform for Software Engineers\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/#\\\/schema\\\/person\\\/a40e0dd38feee7c2779460a804d49361\",\"name\":\"rajeshkumar\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/82337e1fc8642c7f36795aa0775af055a4f38882994004b593efd31758d4434e?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/82337e1fc8642c7f36795aa0775af055a4f38882994004b593efd31758d4434e?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/82337e1fc8642c7f36795aa0775af055a4f38882994004b593efd31758d4434e?s=96&d=mm&r=g\",\"caption\":\"rajeshkumar\"},\"url\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/author\\\/rajeshkumar\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Replace\/Append Cookies in JavaScript - scmGalaxy","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.scmgalaxy.com\/tutorials\/replace-append-cookies-in-javascript\/","og_locale":"en_US","og_type":"article","og_title":"Replace\/Append Cookies in JavaScript - scmGalaxy","og_description":"Replace\/Append Cookies When we assign a new cookie value to document.cookie, the current cookie is not replaced. The new cookie [&hellip;]","og_url":"https:\/\/www.scmgalaxy.com\/tutorials\/replace-append-cookies-in-javascript\/","og_site_name":"scmGalaxy","article_published_time":"2021-08-14T12:56:29+00:00","article_modified_time":"2022-04-13T16:30:44+00:00","author":"rajeshkumar","twitter_card":"summary_large_image","twitter_misc":{"Written by":"rajeshkumar","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.scmgalaxy.com\/tutorials\/replace-append-cookies-in-javascript\/#article","isPartOf":{"@id":"https:\/\/www.scmgalaxy.com\/tutorials\/replace-append-cookies-in-javascript\/"},"author":{"name":"rajeshkumar","@id":"https:\/\/www.scmgalaxy.com\/tutorials\/#\/schema\/person\/a40e0dd38feee7c2779460a804d49361"},"headline":"Replace\/Append Cookies in JavaScript","datePublished":"2021-08-14T12:56:29+00:00","dateModified":"2022-04-13T16:30:44+00:00","mainEntityOfPage":{"@id":"https:\/\/www.scmgalaxy.com\/tutorials\/replace-append-cookies-in-javascript\/"},"wordCount":206,"commentCount":0,"keywords":["Append","Cookies","Javascript","Replace"],"articleSection":["JavaScript"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.scmgalaxy.com\/tutorials\/replace-append-cookies-in-javascript\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.scmgalaxy.com\/tutorials\/replace-append-cookies-in-javascript\/","url":"https:\/\/www.scmgalaxy.com\/tutorials\/replace-append-cookies-in-javascript\/","name":"Replace\/Append Cookies in JavaScript - scmGalaxy","isPartOf":{"@id":"https:\/\/www.scmgalaxy.com\/tutorials\/#website"},"datePublished":"2021-08-14T12:56:29+00:00","dateModified":"2022-04-13T16:30:44+00:00","author":{"@id":"https:\/\/www.scmgalaxy.com\/tutorials\/#\/schema\/person\/a40e0dd38feee7c2779460a804d49361"},"breadcrumb":{"@id":"https:\/\/www.scmgalaxy.com\/tutorials\/replace-append-cookies-in-javascript\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.scmgalaxy.com\/tutorials\/replace-append-cookies-in-javascript\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.scmgalaxy.com\/tutorials\/replace-append-cookies-in-javascript\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.scmgalaxy.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Replace\/Append Cookies in JavaScript"}]},{"@type":"WebSite","@id":"https:\/\/www.scmgalaxy.com\/tutorials\/#website","url":"https:\/\/www.scmgalaxy.com\/tutorials\/","name":"scmGalaxy","description":"A Platform for Software Engineers","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.scmgalaxy.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.scmgalaxy.com\/tutorials\/#\/schema\/person\/a40e0dd38feee7c2779460a804d49361","name":"rajeshkumar","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/82337e1fc8642c7f36795aa0775af055a4f38882994004b593efd31758d4434e?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/82337e1fc8642c7f36795aa0775af055a4f38882994004b593efd31758d4434e?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/82337e1fc8642c7f36795aa0775af055a4f38882994004b593efd31758d4434e?s=96&d=mm&r=g","caption":"rajeshkumar"},"url":"https:\/\/www.scmgalaxy.com\/tutorials\/author\/rajeshkumar\/"}]}},"_links":{"self":[{"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/posts\/7957","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/comments?post=7957"}],"version-history":[{"count":1,"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/posts\/7957\/revisions"}],"predecessor-version":[{"id":7958,"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/posts\/7957\/revisions\/7958"}],"wp:attachment":[{"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/media?parent=7957"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/categories?post=7957"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/tags?post=7957"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}