我试图用javascript设置一个cookie,这就是我到目前为止所做的。
function Set_Cookie( name, value, expires, path, domain, secure )
{
// set time, it's in milliseconds
var today = new Date();
today.setTime( today.getTime() );
/*
if the expires variable is set, make the correct
expires time, the current script below will set
it for x number of days, to make it for hours,
delete * 24, for minutes, delete * 60 * 24
*/
if ( expires )
{
expires = expires * 1000 * 60 * 60 * 24;
}
var expires_date = new Date( today.getTime() + (expires) );
document.cookie = name + "=" +escape( value ) +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
( ( path ) ? ";path=" + path : "" ) +
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );
}
并调用上述方法将cookie设置为
Set_Cookie( "IsLoggedIn", "true", 15, "/", ".spodemo.com", "secure" );
这给了我一个奇怪的问题。在FireFox,Internet Explorer中工作,不在chrome中工作。
对此有任何帮助或建议吗?
感谢。
答案 0 :(得分:2)
我过去曾使用过这种原始js cookie设置
function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
function eraseCookie(name) {
createCookie(name,"",-1);
}
答案 1 :(得分:0)
你在另一台电脑上测试过吗?另外,更改您的JQuery版本。我有同样的问题,它解决了这个问题。
答案 2 :(得分:0)
你不要改变域名和安全。没有他们工作好,即。下方。
function Set_Cookie( name, value, expires, path )
{
// set time, it's in milliseconds
var today = new Date();
today.setTime( today.getTime() );
/*
if the expires variable is set, make the correct
expires time, the current script below will set
it for x number of days, to make it for hours,
delete * 24, for minutes, delete * 60 * 24
*/
if ( expires )
{
expires = expires * 1000 * 60 * 60 * 24;
}
var expires_date = new Date( today.getTime() + (expires) );
document.cookie = name + "=" +escape( value ) +
( ( expires ) ? "; expires=" + expires_date.toGMTString() : "" ) +
( ( path ) ? "; path=" + path : "" );
}