简体   繁体   English

javascript 设置 cookie 在 chrome android 上不起作用

[英]javascript set cookie doesn't work on chrome android

i have a problem to set a cookie on chrome on my android phone.我在 Android 手机上的 chrome 上设置 cookie 时遇到问题。 The source code is upload on an online server.源代码上传到在线服务器上。

here my javascript code :这是我的 javascript 代码:

function updateCookie( value ) {
        document.cookie = 'l='+value+'; expires=Thu, 2 Aug 240 20:47:11 UTC; path=/';
        location.reload();
    }

it work on chrome/mozilla on my computer它适用于我电脑上的 chrome/mozilla
it work on mozilla on my android phone它在我的 android 手机上的 mozilla 上工作
but it doesen't work on chrome on my android phone但它在我的 android 手机上的 chrome 上不起作用

Can sommeone have an issue ?有人可以有问题吗?

I found the problem : the date was invalid.我发现了问题:日期无效。
I modify my function to this我将我的功能修改为此

function updateCookie( langue ) {
        // today + 1 year
        var exdate = new Date().getTime() + (1000*60*60*24*7*52);
        var date_cookie = new Date(exdate).toUTCString();
        document.cookie = 'l='+value+'; expires='+date_cookie+'; path=/';
        location.reload();
    }

And that work on chrome/mozilla on computer and android这适用于计算机和安卓系统上的 chrome/mozilla

I noticed that on a website with https and SameSite="None", cookies were not getting set.我注意到在带有 https 和 SameSite="None" 的网站上,没有设置 cookie。 When I added the "secure" attribute, it worked:当我添加“安全”属性时,它起作用了:

var is_ssl = window.location.protocol === "https:";

var ss = is_ssl ? ";SameSite=None" : ";SameSite=Lax";
var sec = is_ssl ? ";secure" : "";

document.cookie = name + " = " + value + ss + sec;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM