简体   繁体   English

用JavaScript保存cookie如何指定域?

[英]Save cookies with javascript how to specify domain?

I'm using this code to save cookies: 我正在使用以下代码保存Cookie:

function saveCookie(name,value) {
        var date = new Date();
        date.setTime(date.getTime()+(60*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
        document.cookie = name+"="+value+expires+"; path=/";
        }

My problem is that it saves the cookie with domain "example.com" and I want to write them to ".example.com" so I can also read them from subdomains. 我的问题是它将Cookie保存在域“ example.com”中,我想将其写入“ .example.com”,这样我也可以从子域中读取它们。 This is easy to do with PHP but I don't know how to do it with javascript. 使用PHP很容易做到这一点,但我不知道如何使用javascript。 How can I add a dot before the domain when I save the cookie? 保存Cookie时,如何在域之前添加点?

您已经在其中了path ,以相同的方式指定domain

要允许从其他子域读取,请尝试:

'; path=/; domain=.'+window.location.host;

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

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