0

I have been looking at some sites about js cookies such as:

http://www.w3schools.com/js/js_cookies.asp
http://www.quirksmode.org/js/cookies.html Cannot list more links because of reputation.

I could not find anymore useful websites. The ones above don't work for me or are too complicated. I have found the quirksmode website useful but it didn't work. Here's the code I made to test it:

<html> <head> <script> function read() { var x = readCookie('scon') if (x) { alert(x); } } function readCookie(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 createCookie() { document.cookie = 'scon=testcookie; expires=Thu, 17 Jan 2013 13:47:11 UTC; path=/' } </script> <title>Page Title</title> </head> <body> <button onclick="createCookie()">Create</button> <button onclick="read()">Read</button> </body> </html> 

Please can you see whats going wrong and try to explain why and which bit does what if possible. This is going to be used as a standard cookie to save a setting.

2
  • 2
    I could not find anymore useful websites That's just one useful site - quirksmode.org (w3fools.com) ;) For JavaScript the "reference" is MDN. And for your particular problem: document.cookie Commented Jan 16, 2013 at 18:27
  • 1
    What is the problem? Are you running it off of your local computer? AKA c:\foo\bar.html Commented Jan 16, 2013 at 18:31

1 Answer 1

2

What website browser are you using?

If you are using Firefox then it should just work, but Chrome and maybe others don't support "local" cookies... meaning that you have to run the JavaScript on an actual local server or on an actual web host.

Try running your code in the latest Firefox... it worked for me.

Hope that makes sense

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, that would explain it I was using chrome.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.