Example Cookie full (ok)
Ask QuestionAsked 9 years, 5 months agoModified 1 year, 11 months agoViewed 1.4m times678197This question already has answers here:How do I create and read a value from cookie with javascript? (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:
9
kaka = "Css=document.getElementById("css").href" + kakdatum;
is a syntax error. – Bergi Jan 28, 2013 at 23:37what i wonder is how to set cookie based on a choice. If i choose a specific css file, then i want that file to be saved and activated the next time i open the html file – DrWooolie Jan 28, 2013 at 23:41
@DrWooolie How about marking an answer correct so visitors to this question will find the best answer? The top answer clearly isn't the best. – JohnAllen May 12, 2014 at 20:00
Now (2017), some use cases make investigating the Web Storage API, as opposed to cookies, worthwile. – MattBianco Aug 3, 2017 at 9:02
A concise but fully featured modern approach to get/set cookies over at the duplicate question: stackoverflow.com/a/48706852/87520 – SamGoody Feb 19, 2018 at 9:32
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.html
They updated the page today so everything in the page should be latest as of now.
7
toGMTString()
is deprecated - just FYI. Reference – Keith W. Nov 20, 2015 at 19:4814This won't work if your cookie contains a semicolon. – lucaswxp Jan 11, 2016 at 21:31
7Why so complex? Use
date.setDate(date.getDate() + days);
instead – dude Mar 8, 2018 at 15:225The eraseCookie function didn't work for me (FireFox Developer Edition 66.0b4). Instead, I had to use the code from B T's answer:
document.cookie = name + '=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/'
. – ban-geoengineering Feb 1, 2019 at 20:284Nice answer. While using semicolon(;) in cookie value I have got problem. So I refined some lines. Please update in your answer. From function setCookie(name,value,days)
document.cookie = name + "=" + (encodeURIComponent(value) || "") + expires + "; path=/";
From function getCookie(name)if (c.indexOf(nameEQ) == 0) return decodeURIComponent(c.substring(nameEQ.length,c.length));
UseencodeURIComponent(), decodeURIComponent()
in retutn statement; – KarthikeyanMlp Mar 1, 2019 at 13:36
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.
28The expires variable is obsolete although still supported by browsers. Use max-age instead! – tyler Jul 25, 2013 at 1:34
61It looks like IE8 and below do not support
max-age
, soexpires
is the safer choice. blogs.msdn.com/b/ieinternals/archive/2009/08/20/… – thaddeusmt Oct 8, 2013 at 16:295There's also a
domain
param you can set on cookies which is useful if you want to use a cookie across different sub domains on your site. – Alex W Oct 25, 2013 at 20:262Dont work on Chrome. See here why: stackoverflow.com/questions/26349052/… – Peter Apr 6, 2018 at 14:10
2Note that the MDN reference page mentions nothing about
expires
being deprecated/obsolete. That's because it can, on occasion, serve a purpose not covered bymax-age
. The value ofmax-age
must be a numerical value interpreted as an amount of seconds, while theexpires
value can be set to the special valueSession
which is not the same asmax-age=0;
. – ocket8888 May 4, 2018 at 17:57
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:
13@BT could you elaborate on what about the above code is out of date and misinformed? – Justin Dec 10, 2013 at 19:08
14@BT, I've yet to see a reference on w3schools that is out of date or contains incorrect information. – Cypher Jan 8, 2014 at 1:14
17-1 for w3schools – ohcibi Jan 13, 2014 at 12:09
10I just checked our analytics, and 10% of our IE users still use IE8 or lower. So using 'max-age' is a pretty bad idea. – CpnCrunch Nov 12, 2014 at 19:32
18Stupidness!! +1 for the working example, I don't actually see a problem with w3schools, – amd Mar 27, 2015 at 10:03
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 MDN 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 here:
6Just a word of warning @SandipPingle, the code provided in this answer is actually GPL licensed. – jahu Aug 9, 2014 at 16:03
@MarcinHabuszewski thanks for pointing out. How can I use this code then(my app is not GPL licensed)? – Sandip Pingle Aug 11, 2014 at 5:55
8@SandipPingle That's the "charm" of GPL, you can't (unless you turn your app GPL). This code is somewhat similar to a generic solution to the problem, so it begs for a question: can generic code actually be licensed (or is such license valid in case of generic code)? Try using different (non-GPLed) code to solve your problem. – jahu Aug 11, 2014 at 11:38
18GPL or not, I prefer code I can read. – Cypher Mar 12, 2015 at 0:15
setItem
does not work for me. I tested it on chrome. the other methods work fine. – IgniteCoders Jun 22, 2015 at 16:34
Not the answer you're looking for? Browse other questions tagged javascript html css cookies or ask your own question.
Last updated