Trying to test the storage of cookies for a site I'm writing. Whenever I try reading a cookie to check for its existence, the console gives me nothing in return.
function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
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 eraseCookie(name) {
createCookie(name,"",-1);
}
createCookie("name","value",7);
if (readCookie("name")){
console.log("it works");
};
This can occur if your code is served from file://
instead of http://
protocol.
Opera (and Chrome) doesn't save cookies if there is no domain and http communication.
Knowing the above, your options are:
--enable-file-cookies
flag (This works on Chrome for me, but not sure about Opera)Serve your file over http server. If you're on MAC, it's as simple as:
$ cd /project_folder/
$ python -m SimpleHTTPServer 8000
where /project_folder/
contains your html file(s).
You can then use any browser to access http://localhost:8000/your_html_file.html
Source: https://bugs.chromium.org/p/chromium/issues/detail?id=535