js 設定cookie

來源:互聯網
上載者:User

我試圖設定一個cookie,但我無法得到它的工作。
也有人請你指出為什嗎?
當點擊了函數create_cookie被調用,但不是該Cookie。

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head><title>JS Testing Page</title>
<script type="text/javascript">
onError=errorhandler;
function errorhandler(msg,url,l)
{
text = "There was an error. n";
text+= "The error was : " + msg + " n";
text+= "It happened on " + url + " n ";
text+= "on line " + l + ".";
alert(text);
}

function readCookie()
{
    var cvalue = document.cookie;
    if(cvalue !== null || cvalue!= "")
    {
    return cvalue;
    }
    else
    {
    return false;
    }
}   
function delete_cookies()
{
var cnames = document.cookie;

for(var i in cnames)
{
    var exp_Date = new Date();
    document.cookie = cnames.i + "=""; expires=" + (exp_Date-1) + ";";
}
return true;
}
function create_cookie()
{
    var exp_Date = new Date();
    /*document.write(exp_Date);*/
    document.cookie = "test=yes; expires="+exp_Date+";";
    document.write("Lo <br />");
    return true;
}
</script>
</head>
<body>
<script type="text/javascript">
var x = readCookie();
document.write(x);
if((x != "") && (x !== null) && (x !== false))
{
document.write("<br /> The cookie that has been set was : " + x);
document.write("<br /><a href="#" onclick="delete_cookies(); document.location.reload();">Clear cookies stored by this site.</a>");
}
else if( x== "" || x === null || x === false)
{
document.write("<br /> <a href="#" onclick="create_cookie()">Set</a> the cookie.");
alert("Hello");
}
</script>
</body>
</html>


您沒有設定Cookie失效日期,正確。

設定為在特定天數到期使用

var exp_Date = new Date ();
var days =1;
exp_Date.setTime(exp_Date.getTime()+(days*24*60*60*1000));

document.cookie = "test=yes; expires=" + exp_Date.toGMTString();

to last till your current session only, remove all code for date, and just use

document.cookie = "test=yes;";


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>JS Testing Page</title>
<script type="text/javascript">

function errorhandler(msg,url,l)
{
text = "There was an error. n";
text+= "The error was : " + msg + " n";
text+= "It happened on " + url + " n ";
text+= "on line " + l + ".";
alert(text);
}
onError=errorhandler;
function readCookie()
{
    alert("ja");
    var cvalue = document.cookie;
   
    if(cvalue!==false || cvalue!==null || cvalue!='')
    {
        if(cvalue !== null || cvalue != "")
        {
        return cvalue;
        }
        else
        {
        return false;
        }
    }
}   
function delete_cookies()
{
//var cnames/*_data*/ = document.cookie;
//var cnames = cname_data.split('=');
if(document.cookie !== null && document.cookie != "")
{
    var line = document.cookie.indexOf("=");
    var begin = 0;
    var cname = document.cookie.slice(begin,line);
    var exp_Date = new Date();
    exp_Date.setTime(exp_Date.getTime()-1000*60*60);
    if(document.cookie.indexOf('=') == document.cookie.lastindexOf('='))
    {
        document.cookie = cname + "=''; expires=" + exp_Date + ";";
        //alert("Cookie " + cname + " has been deleted.");
    }       
   
    else
    {
    var cookieval = cname + "=""; ";
    cookieval += "expires=" + exp_Date + ";";
    document.cookie = cookieval;
    cname = document.cookie;
    if(cname =='' || cname === false || cname === null)
    {
        break;
    }
   
    }
return true;
}
}

function create_cookie(cookval)
{
    var exp_Date = new Date();
    exp_Date.setTime(exp_Date.getTime()+1000*60*60);
    /*document.write(exp_Date);*/
    var cookieval = "user=" + cookval + "; ";
    cookieval += "expires=" + exp_Date + ";";
    document.cookie = cookieval;
    return true;
}
</script>
</head>
<body>
<script type="text/javascript">
var x = readCookie();
if(x != "" && x !== null && x !== false)
{
document.write("<br /> The cookie that has been set was : " + x);
document.write("<br /><a href="#" onclick="delete_cookies(); document.location.reload();">Clear cookies stored by this site.</a>");
}
else if( x== "" || x === null || x === false)
{
document.write("<br /> <a href="#" onclick="create_cookie('cookie')">Set</a> the cookie.");
}
</script>
</body>
</html>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.