使用javascript设置持久性Cookie
我在我的应用程序中发现了一个奇怪的错误,由于我的Javascript经验,我无法调试它;
I have found a weird bug in my application and due to my small experience with Javascript I couldn't debug it;
我试图设置一个持久性cookie,它将在设置一年后(在主要浏览器中的最大值)死亡,但仍然存在,并且不会在浏览器关闭,我一直在使用此代码:
I am trying to set a persistent cookie, which will die after one year from its set (max value in major browsers) but persists and won't be deleted after the browser gets closed, I've been using this code :
// Build the expiration date string:
var expiration_date = new Date ();
expiration_date . setYear (expiration_date . getYear () + 1);
expiration_date = expiration_date . toGMTString ();
// Build the set-cookie string:
var cookie_string = "test_cookies = true; path=/; expires=" + expiration_date;
// Create/update the cookie:
document . cookie = cookie_string;
我注意到,当我使用cookie管理器插件时,cookie有会话标签,
I've noticed that the cookie has session tag when I use cookie manager plugin, and only the ones with this tag gets removed after browser shut down (others like Wordpress's and such scripts persist)
我已将您的语法更改为我的风格的编码(变量在顶部,最小的重建等),下面的例子在我的localhost很好。
I changed your syntax over to my style of coding (variables at the top, minimal re-casting, etc.) and the example below works on my localhost quite well.
// Build the expiration date string:
var expiration_date = new Date();
var cookie_string = '';
expiration_date.setFullYear(expiration_date.getFullYear() + 1);
// Build the set-cookie string:
cookie_string = "test_cookies=true; path=/; expires=" + expiration_date.toUTCString();
// Create or update the cookie:
document.cookie = cookie_string;
如果您在生产服务器上遇到问题,请尝试设置Cookie的域a href =http://www.quirksmode.org/js/cookies.html#link5 =nofollow> www.quirksmode.org/js/cookies.html#link5 )
If you are having problems on a production server, try setting the domain of the cookie as well (www.quirksmode.org/js/cookies.html#link5)