i try to learn how to set cookies but when i try to open my page with chrome/IE is like there are no cookies .. but in firefox all work ! i have try to remove "sameSite attribute et secure" and that work on all navigators but i have an error message in firefox like : "you need to give a sameSite attribute etc etc .." Someone can help me ? this is my code
function creerCookie(result){
document.cookie = 'Cookie='+result+ '; SameSite:"Lax";secure '
console.log(document.cookie)
}
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;
}
let body = document.body
let kiki = getCookie('Cookie')
console.log("voila :"+kiki)
if (kiki==="test1"){
body.style.background = "red"
}else if (kiki === "test2"){
body.style.background = "green"
}
The issue is with the Secure
attribute. From this doc , we can know that
A cookie with the
Secure
attribute is sent to the server only with an encrypted request over the HTTPS protocol, never with unsecured HTTP.Insecure sites (with
http:
in the URL) can't set cookies with theSecure
attribute.
You can host your test page via https
then the code will work in IE and Chrome.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.