Example Cookie full (ok)

// functions cookie
function create_cookie(name, value, days) { 
    if (days) { 
        var date = new Date(); 
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000)); 
        var expires = "; expires=" + date.toGMTString(); 
        document.cookie = name + "=" + value + expires + "; path=/"; 
    } else { 
        document.cookie = name + "=" + value; 
    } 
}

function get_cookie(cname) {
    var name = cname + "=";
    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);
        if (c.indexOf(name) == 0) return c.substring(name.length,c.length);
    }
    return "";
}
function remove_cookie(name) {
    document.cookie = name + '=;expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/';
}

Ask Questionarrow-up-rightAsked 9 years, 5 months agoModified 1 year, 11 months agoarrow-up-rightViewed 1.4m times678197This question already has answers here:How do I create and read a value from cookie with javascript?arrow-up-right (24 answers)Closed 7 years ago.

I'm trying to set a cookie depending on which CSS file I choose in my HTML. I have a form with a list of options, and different CSS files as values. When I choose a file, it should be saved to a cookie for about a week. The next time you open your HTML file, it should be the previous file you've chosen.

JavaScript code:

HTML code:

javascriptarrow-up-righthtmlarrow-up-rightcssarrow-up-rightcookiesarrow-up-rightSharearrow-up-rightImprove this questionarrow-up-rightFollowedited Mar 1, 2018 at 21:53arrow-up-rightuser avatararrow-up-rightAaron Martinarrow-up-right12811 silver badge1313 bronze badgesasked Jan 28, 2013 at 23:34user avatararrow-up-rightDrWoooliearrow-up-right7,05977 gold badges1919 silver badges1919 bronze badges

Show 1 more commentarrow-up-right

4 Answers

Sorted by:Trending sort available Highest score (default) Trending (recent votes count more) Date modified (newest first) Date created (oldest first) 1053

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

Now, calling functions

Source - http://www.quirksmode.org/js/cookies.htmlarrow-up-right

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

Sharearrow-up-rightImprove this answerarrow-up-rightFollowedited Jul 12, 2020 at 14:27arrow-up-rightuser avatararrow-up-rightFakhruddin Ujjainwalaarrow-up-right2,4631616 silver badges2626 bronze badgesanswered Jun 8, 2014 at 6:28user avatararrow-up-rightMandeep Janjuaarrow-up-right14.4k44 gold badges2727 silver badges2424 bronze badges

Show 19 more commentsarrow-up-right424

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

Examples derived from these references:

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

Sharearrow-up-rightImprove this answerarrow-up-rightFollowedited Aug 3, 2020 at 11:35arrow-up-rightuser avatararrow-up-rightlinarrow-up-right17.5k44 gold badges5555 silver badges8282 bronze badgesanswered Jul 8, 2013 at 8:18user avatararrow-up-rightB Tarrow-up-right53k3434 gold badges176176 silver badges200200 bronze badges

Show 2 more commentsarrow-up-right38

Check JavaScript Cookies on W3Schools.comarrow-up-right for setting and getting cookie values via JS.

Just use the setCookie and getCookie methods mentioned there.

So, the code will look something like:

Sharearrow-up-rightImprove this answerarrow-up-rightFollowedited Sep 24, 2016 at 9:41arrow-up-rightuser avatararrow-up-rightStackedarrow-up-right6,32466 gold badges5555 silver badges7373 bronze badgesanswered Jan 29, 2013 at 0:20user avatararrow-up-rightMunish Pooniaarrow-up-right77844 silver badges33 bronze badges

Show 4 more commentsarrow-up-right36

I'm sure this question should have a more general answer with some reusable code that works with cookies as key-value pairs.

This snippet is taken from MDNarrow-up-right and probably is trustable. This is UTF-safe object for work with cookies:

Mozilla has some tests to prove this works in all cases.

There is an alternative snippet herearrow-up-right:

Sharearrow-up-rightImprove this answerarrow-up-rightFollowedited Jun 4, 2014 at 14:20arrow-up-rightuser avatararrow-up-rightCommunityarrow-up-rightBot111 silver badgeanswered Jun 4, 2014 at 11:09user avatararrow-up-rightDanarrow-up-right52.7k3939 gold badges111111 silver badges150150 bronze badges

Show 5 more commentsarrow-up-right

Last updated