javascript 一个 Cookie - 多页

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8109437/
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 02:19:08  来源:igfitidea点击:

One Cookie - Multiple Pages

javascriptcookies

提问by L84

I am using the following cookie:

我正在使用以下 cookie:

var $j = jQuery.noConflict();

$j(document).ready(function(){

   if (document.cookie.indexOf('visited=true') == -1) 
   {
      var thirtyDays = 1000*60*60*24*30;
      var expires = new Date((new Date()).valueOf() + thirtyDays);
      document.cookie = "visited=true;expires=" + expires.toUTCString();
      $j.colorbox({ inline:true, href:"#gallery-nav-instruct"});
   }

});

Everything works fine with one exception. The above cookie is for displaying instructions the first time a user visit the gallery yet the gallery has multiple pages. What happens is the user sees the instructions for each page in the gallery the first time they visit that specific page. These instructions need to load only once when they visit the gallery no matter which page they start on. How do I go about changing this so it displays only once across my gallery pages?

一切正常,只有一个例外。上述 cookie 用于在用户第一次访问画廊时显示说明,但画廊有多个页面。发生的情况是用户在第一次访问特定页面时会看到图库中每个页面的说明。无论他们从哪个页面开始,这些说明只需要在他们访问画廊时加载一次。我该如何改变它,让它在我的画廊页面上只显示一次?

Couple Notes:

情侣注意事项:

The gallery is wrapped inside a Dreamweaver Template and the cookie is inside that template. I cannot move the cookie outside of the template for a few reasons.

图库包含在 Dreamweaver 模板中,cookie 位于该模板中。由于一些原因,我无法将 cookie 移到模板之外。

Also I use a hosted CMS and I DO NOThave server side access so it must be done using javascript.

此外,我使用托管 CMS,但我没有服务器端访问权限,因此必须使用 javascript 完成。

回答by David Schwartz

Add ;path=/to make your cookie into a site cookie. See this article on JavaScript Cookiesfor more details.

添加;path=/使您的 cookie 成为站点 cookie。有关更多详细信息,请参阅有关JavaScript Cookie 的这篇文章。

回答by Abhishek SInha

document.cookie = valuename + "=" + value + "; " + expires + ";domain=;path=/";

This "domain=;path=/";will take dynamic domain as its cookie will work in subdomain. It will work if you want to test in localhost.

"domain=;path=/";将采用动态域,因为它的 cookie 将在子域中工作。如果您想在 localhost 中进行测试,它将起作用。