Set cookie and get cookie with JavaScript [duplicate] Set cookie and get cookie with JavaScript [duplicate] javascript javascript

Set cookie and get cookie with JavaScript [duplicate]


I find the following code to be much simpler than anything else:

function setCookie(name,value,days) {    var expires = "";    if (days) {        var date = new Date();        date.setTime(date.getTime() + (days*24*60*60*1000));        expires = "; expires=" + date.toUTCString();    }    document.cookie = name + "=" + (value || "")  + expires + "; path=/";}function getCookie(name) {    var nameEQ = name + "=";    var ca = document.cookie.split(';');    for(var i=0;i < ca.length;i++) {        var c = ca[i];        while (c.charAt(0)==' ') c = c.substring(1,c.length);        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);    }    return null;}function eraseCookie(name) {       document.cookie = name +'=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;';}

Now, calling functions

setCookie('ppkcookie','testcookie',7);var x = getCookie('ppkcookie');if (x) {    [do something with x]}

Source - http://www.quirksmode.org/js/cookies.html

They updated the page today so everything in the page should be latest as of now.


These are much much better references than w3schools (the most awful web reference ever made):

Examples derived from these references:

// sets the cookie cookie1document.cookie = 'cookie1=test; expires=Sun, 1 Jan 2023 00:00:00 UTC; path=/'// sets the cookie cookie2 (cookie1 is *not* overwritten)document.cookie = 'cookie2=test; expires=Sun, 1 Jan 2023 00:00:00 UTC; path=/'// remove cookie2document.cookie = 'cookie2=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/'

The Mozilla reference even has a nice cookie library you can use.


Check JavaScript Cookies on W3Schools.com for setting and getting cookie values via JS.

Just use the setCookie and getCookie methods mentioned there.

So, the code will look something like:

<script>function setCookie(c_name, value, exdays) {    var exdate = new Date();    exdate.setDate(exdate.getDate() + exdays);    var c_value = escape(value) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString());    document.cookie = c_name + "=" + c_value;}function getCookie(c_name) {    var i, x, y, ARRcookies = document.cookie.split(";");    for (i = 0; i < ARRcookies.length; i++) {        x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("="));        y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);        x = x.replace(/^\s+|\s+$/g, "");        if (x == c_name) {            return unescape(y);        }    }}function cssSelected() {    var cssSelected = $('#myList')[0].value;    if (cssSelected !== "select") {        setCookie("selectedCSS", cssSelected, 3);    }}$(document).ready(function() {    $('#myList')[0].value = getCookie("selectedCSS");})</script><select id="myList" onchange="cssSelected();">    <option value="select">--Select--</option>    <option value="style-1.css">CSS1</option>    <option value="style-2.css">CSS2</option>    <option value="style-3.css">CSS3</option>    <option value="style-4.css">CSS4</option></select>