为整个域而不是特定页面设置cookie(使用JS) [英] Set cookie (with JS) for whole domain not specific page

查看:142
本文介绍了为整个域而不是特定页面设置cookie(使用JS)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的小脚本,用于设置cookie:

I have a simple little script which I am using to set a cookie:

function setCookie(cname, cvalue, exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays*24*60*60*1000));
    var expires = "expires="+d.toUTCString();
    document.cookie = cname + "=" + cvalue + "; " + expires;
}

我有这个cookie的问题只在一个页面上设置,而不是在整个域。

The problem I have this cookie is only set on one page, not across the whole domain.

如何调整此功能以使cookie保留在整个域中?

How can I adjust this function so that the cookie remains across the whole domain?

推荐答案

您可以指定域; domain = .example.com 以及路径; path = / (/在整个域中设置cookie)

You can specifiy domain ;domain=.example.com as well as path ;path=/ ("/" set cookie in whole domain)

document.cookie = cname + "=" + cvalue + "; " + expires +";path=/";

这篇关于为整个域而不是特定页面设置cookie(使用JS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆