[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"\u002Fblog\u002Fjavascript-cookie-not-working-other-page":3,"$flqBtCDkx0hnriwgdalHKewdQDcOrE0PQeh-JSQjTKQ0":158,"i-local-icon:calendar-days":162,"i-local-icon:tag":167,"i-local-icon:eye":169},{"id":4,"title":5,"body":6,"categories_slug":146,"comment_count":149,"date_published":150,"description":26,"extension":151,"meta":152,"navigation":153,"path":154,"seo":155,"stem":156,"__hash__":157},"content\u002Fblog\u002F5.javascript-cookie-not-working-other-page.md","Javascript cookie not working on another page",{"type":7,"value":8,"toc":142},"minimark",[9,13,18,23,27,39,45,48,85,88,92,101,104,138],[10,11,5],"h1",{"id":12},"javascript-cookie-not-working-on-another-page",[14,15],"meta-card",{":categories":16,":date":17},"categories_slug","date_published",[19,20,22],"h2",{"id":21},"solution","Solution",[24,25,26],"p",{},"When you set a cookie you should specify an expire time and a path. If you don’t specify a path the cookie is accessible only by the current page.",[24,28,29,30,38],{},"As per the ",[31,32,37],"a",{":target":33,"href":34,"rel":35},"_blank","https:\u002F\u002Fdeveloper.mozilla.org\u002Fen-US\u002Fdocs\u002FWeb\u002FAPI\u002Fdocument\u002Fcookie",[36],"nofollow","MDN Web docs",":",[24,40,41],{},[42,43,44],"em",{},"Cookie path – If not specified, defaults to the current path of the current document location.",[24,46,47],{},"An example of setting a maximum age and path:",[49,50,55],"pre",{"className":51,"code":52,"language":53,"meta":54,"style":54},"language-js shiki shiki-themes github-dark","\u002F\u002F Set cookie with path.\ndocument.cookie = \"theme=dark; Max-Age=604800; path=\u002F\";\n","js","",[56,57,58,67],"code",{"__ignoreMap":54},[59,60,63],"span",{"class":61,"line":62},"line",1,[59,64,66],{"class":65},"sAwPA","\u002F\u002F Set cookie with path.\n",[59,68,70,74,78,82],{"class":61,"line":69},2,[59,71,73],{"class":72},"s95oV","document.cookie ",[59,75,77],{"class":76},"snl16","=",[59,79,81],{"class":80},"sU2Wk"," \"theme=dark; Max-Age=604800; path=\u002F\"",[59,83,84],{"class":72},";\n",[24,86,87],{},"By setting path=\u002F, the cookie is now available from every page in your application\u002Fdomain. If you do not specify the path then the current cookie is saved just for the current page and you can’t access it from other pages.",[19,89,91],{"id":90},"alternative-option","Alternative option",[24,93,94,95,100],{},"A handy, lightweight JavaScript API for handling cookies is ",[31,96,99],{":target":33,"href":97,"rel":98},"https:\u002F\u002Fgithub.com\u002Fjs-cookie\u002Fjs-cookie",[36],"available here",".",[24,102,103],{},"If using this API, then you can create a cookie, valid across the entire site with the following code:",[49,105,107],{"className":51,"code":106,"language":53,"meta":54,"style":54},"\u002F\u002F Set cookie.\nCookies.set('name', 'value');\n",[56,108,109,114],{"__ignoreMap":54},[59,110,111],{"class":61,"line":62},[59,112,113],{"class":65},"\u002F\u002F Set cookie.\n",[59,115,116,119,123,126,129,132,135],{"class":61,"line":69},[59,117,118],{"class":72},"Cookies.",[59,120,122],{"class":121},"svObZ","set",[59,124,125],{"class":72},"(",[59,127,128],{"class":80},"'name'",[59,130,131],{"class":72},", ",[59,133,134],{"class":80},"'value'",[59,136,137],{"class":72},");\n",[139,140,141],"style",{},"html pre.shiki code .sAwPA, html code.shiki .sAwPA{--shiki-default:#6A737D}html pre.shiki code .s95oV, html code.shiki .s95oV{--shiki-default:#E1E4E8}html pre.shiki code .snl16, html code.shiki .snl16{--shiki-default:#F97583}html pre.shiki code .sU2Wk, html code.shiki .sU2Wk{--shiki-default:#9ECBFF}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html pre.shiki code .svObZ, html code.shiki .svObZ{--shiki-default:#B392F0}",{"title":54,"searchDepth":69,"depth":69,"links":143},[144,145],{"id":21,"depth":69,"text":22},{"id":90,"depth":69,"text":91},[147,148],"development","javascript",null,"2021-12-26","md",{},true,"\u002Fblog\u002Fjavascript-cookie-not-working-other-page",{"title":5,"description":26},"blog\u002F5.javascript-cookie-not-working-other-page","NVBYgCCDraWKS_80muFHiifdjiR8T5YTuSPXrAWJZ9o",[159],{"id":160,"name":154,"count":161},5,5727,{"left":163,"top":163,"width":164,"height":164,"rotate":163,"vFlip":165,"hFlip":165,"body":166},0,24,false,"\u003Cpath\n    fill=\"none\"\n    stroke=\"currentColor\"\n    stroke-linecap=\"round\"\n    stroke-linejoin=\"round\"\n    stroke-width=\"1.5\"\n    d=\"M6.75 3v2.25M17.25 3v2.25M3 18.75V7.5a2.25 2.25 0 0 1 2.25-2.25h13.5A2.25 2.25 0 0 1 21 7.5v11.25m-18 0A2.25 2.25 0 0 0 5.25 21h13.5A2.25 2.25 0 0 0 21 18.75m-18 0v-7.5A2.25 2.25 0 0 1 5.25 9h13.5A2.25 2.25 0 0 1 21 11.25v7.5m-9-6h.008v.008H12zM12 15h.008v.008H12zm0 2.25h.008v.008H12zM9.75 15h.008v.008H9.75zm0 2.25h.008v.008H9.75zM7.5 15h.008v.008H7.5zm0 2.25h.008v.008H7.5zm6.75-4.5h.008v.008h-.008zm0 2.25h.008v.008h-.008zm0 2.25h.008v.008h-.008zm2.25-4.5h.008v.008H16.5zm0 2.25h.008v.008H16.5z\" \u002F>",{"left":163,"top":163,"width":164,"height":164,"rotate":163,"vFlip":165,"hFlip":165,"body":168},"\u003Cg\n    fill=\"none\"\n    stroke=\"currentColor\"\n    stroke-linecap=\"round\"\n    stroke-linejoin=\"round\"\n    stroke-width=\"1.5\">\n    \u003Cpath\n      d=\"M9.568 3H5.25A2.25 2.25 0 0 0 3 5.25v4.318c0 .597.237 1.17.659 1.591l9.581 9.581c.699.699 1.78.872 2.607.33a18.1 18.1 0 0 0 5.224-5.223c.54-.827.368-1.908-.33-2.607l-9.583-9.58A2.25 2.25 0 0 0 9.568 3\" \u002F>\n    \u003Cpath d=\"M6 6h.008v.008H6z\" \u002F>\n  \u003C\u002Fg>",{"left":163,"top":163,"width":164,"height":164,"rotate":163,"vFlip":165,"hFlip":165,"body":170},"\u003Cg\n    fill=\"none\"\n    stroke=\"currentColor\"\n    stroke-linecap=\"round\"\n    stroke-linejoin=\"round\"\n    stroke-width=\"1.5\">\n    \u003Cpath\n      d=\"M2.036 12.322a1 1 0 0 1 0-.639C3.423 7.51 7.36 4.5 12 4.5c4.638 0 8.573 3.007 9.963 7.178c.07.207.07.431 0 .639C20.577 16.49 16.64 19.5 12 19.5c-4.638 0-8.573-3.007-9.963-7.178\" \u002F>\n    \u003Cpath d=\"M15 12a3 3 0 1 1-6 0a3 3 0 0 1 6 0\" \u002F>\n  \u003C\u002Fg>"]