如何在 javascript 中创建全局 cookie?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11868745/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 14:36:37  来源:igfitidea点击:

How to create a global cookie in javascript?

javascriptcookieswebglobal

提问by SImon

I've created a Django website, and need a cookie to be stored and readable from any part of the site. The javascript for it is in every part I need it in, but for some reason the cookie itself is stored seperately for each page. E.g. if the cookie is equal to "set" on one page, it can be undefined on another. Here's the code I'm using to create, get, and read the cookie (the "createBannerCookie()" method is called when a specific button, found on every page, is pressed)-

我创建了一个 Django 网站,需要一个 cookie 来存储并从网站的任何部分读取。它的 javascript 包含在我需要它的每个部分中,但出于某种原因,cookie 本身为每个页面单独存储。例如,如果 cookie 在一个页面上等于“设置”,则它可以在另一个页面上未定义。这是我用来创建、获取和读取 cookie 的代码(当按下每个页面上的特定按钮时会调用“createBannerCookie()”方法)-

<script type="text/javascript">
$(document).ready(function() {
  $('#banner').hide();
  checkBannerCookie();
});

function createBannerCookie() 
{
  $('#banner').hide();
  var exdate=new Date();
  exdate.setDate(exdate.getDate() + 3);
  var c_value=escape("set") + ((exdate==null) ? "" : "; expires="+exdate.toUTCString());
  document.cookie='banner=' + c_value;
}

function getCookie(c_name)
{
  var i,x,y,ARRcookies=document.cookie.split(";");
  for (i=0;i<ARRcookies.length;i++)
  {
    x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
    y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
    x=x.replace(/^\s+|\s+$/g,"");
    if (x==c_name)
    {
      return unescape(y);
    }
  }
}

function checkBannerCookie()
{
  var banner=getCookie("banner");
  if (banner!=null && banner!="")
  {
    $('#banner').hide();
  }
  else 
  {
    $('#banner').show();
  }
}
</script>

Any suggestions?

有什么建议?

回答by udalmik

By default, cookies are accessible only to web pages in the same directory as the web page which originally created the cookie. Please try to add "path=/" option. e.g.

默认情况下,只有与最初创建 cookie 的网页位于同一目录中的网页才能访问 cookie。请尝试添加“path=/”选项。例如

document.cookie =
  'propertyName=test; path=/'

回答by Matt

SImon,

西蒙,

I think your problem is the expiration date on your cookies. It looks to me like you are setting them to expire 3 miliseconds after being created.

我认为您的问题是 cookie 的到期日期。在我看来,您将它们设置为在创建后 3 毫秒过期。

Try something like this in your "createBannerCookie" function (instead of the w3schools version):

在您的“createBannerCookie”函数(而不是 w3schools 版本)中尝试类似的操作:

function createBannerCookie() 
{
  $('#banner').hide();
  var exdate=new Date();
  exdate.setTime(exdate.getTime()+(3*24*60*60*1000)); // the 3 in that math is your days
  var c_value=escape("set") + ((exdate==null) ? "" : "; expires="+exdate.toUTCString());
  document.cookie='banner=' + c_value;
}

Reference: http://www.quirksmode.org/js/cookies.html

参考:http: //www.quirksmode.org/js/cookies.html