{"id":2362,"date":"2016-03-18T12:44:16","date_gmt":"2016-03-18T19:44:16","guid":{"rendered":"http:\/\/www.wellgolly.com\/?p=2362"},"modified":"2016-03-18T12:44:27","modified_gmt":"2016-03-18T19:44:27","slug":"browser-cookies-with-javascript","status":"publish","type":"post","link":"https:\/\/www.wellgolly.com\/?p=2362","title":{"rendered":"Browser Cookies with JavaScript"},"content":{"rendered":"<p>Using JavaScript to set and get cookies is pretty straightforward. I wrote a simple function to set cookies for a game I\u2019m working on. This code initializes\/resets the cookies.<br \/>\n<pre><code class=\"preserve-code-formatting\">\nvar cookie_days = 30;\nvar cor = 0;\nvar inc = 0;\nvar screen = 0;\nvar level = &quot;MW1&quot;;\nsetCookie(&#039;VPA_cor&#039;,cor,&#039;N&#039;);\nsetCookie(&#039;VPA_inc&#039;,inc,&#039;N&#039;);\nsetCookie(&#039;VPA_screen&#039;,screen,&#039;N&#039;);\nsetCookie(&#039;VPA_level&#039;,level,&#039;Y&#039;);\n\nfunction setCookie(cname, cvalue, set_time) {\nvar expires = &#039;&#039;;\nif ( set_time == &#039;Y&#039; ) {\n&nbsp;&nbsp;&nbsp;&nbsp;var d = new Date();\n&nbsp;&nbsp;&nbsp;&nbsp;d.setTime(d.getTime() + (cookie_days*24*60*60*1000));\n&nbsp;&nbsp;&nbsp;&nbsp;expires = &quot;expires=&quot;+d.toUTCString();\n}\n\/\/alert(&#039;cname &#039; + cname + &#039;group &#039; + cvalue + &#039;time &#039; + expires);\ndocument.cookie = cname + &quot;=&quot; + cvalue + &quot;; &quot; + expires;\n}<\/code><\/pre><\/p>\n<p>Note that the JavaScript code for setting and resetting a cookie is the same. One caveat is that if the cookie has been set outside of JavaScript, e.g. in PHP, then it is possible that the <i>httponly<\/i> has been set to TRUE. In that case you cannot modify an existing cookie with JavaScript.<\/p>\n<p>You can get the value of the cookies with this code.<br \/>\n<pre><code class=\"preserve-code-formatting\">\nvar cookie_name = &#039;VPA_cor&#039;;\nvar cor = retrieve_cookie(cookie_name);<\/code><\/pre><\/p>\n<p>In my code, I set the cookies with PHP, so the path is already set. If you initialize a cookie with JavaScript, you can set the path by adding a path parameter.<br \/>\n<pre><code class=\"preserve-code-formatting\">\n document.cookie = name + &#039;=&#039; + value + &#039;;&#039; +\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#039;expires=&#039; + expires + &#039;;&#039; +\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#039;path=&#039; + path + &#039;;&#039;;<\/code><\/pre><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Using JavaScript to set and get cookies is pretty straightforward. I wrote a simple function to set cookies for a game I\u2019m working on. This code initializes\/resets the cookies. var cookie_days = 30; var cor = 0; var inc = 0; var screen = 0; var level = &quot;MW1&quot;; setCookie(&#039;VPA_cor&#039;,cor,&#039;N&#039;); setCookie(&#039;VPA_inc&#039;,inc,&#039;N&#039;); setCookie(&#039;VPA_screen&#039;,screen,&#039;N&#039;); setCookie(&#039;VPA_level&#039;,level,&#039;Y&#039;); function setCookie(cname, &hellip; <a href=\"https:\/\/www.wellgolly.com\/?p=2362\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Browser Cookies with JavaScript<\/span><\/a><\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[28],"tags":[],"class_list":["post-2362","post","type-post","status-publish","format-standard","hentry","category-html-and-css"],"_links":{"self":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts\/2362","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2362"}],"version-history":[{"count":0,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts\/2362\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2362"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2362"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2362"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}