如何将其转换为会话cookie?

时间:2014-03-14 18:55:26

标签: javascript cookies

如何将其转换为会话cookie?

我知道我会从摆脱exipredays开始,但是当我将expiredays设置为0时,每次刷新页面时都会显示消息cbox,我可以看到它变得很烦人。

<p align="center">DISCLAIMER GOES HERE</p>
<script type="text/javascript" language="javascript">
var agreement = GetCookie();

// checks for cookie and displays disclaimer alert if new user
if(agreement=="")
    {
    var decision = confirm("DISCLAIMER: GOES HERE \n\nClick Ok if you agree to the disclaimer or click Cancel to close this window. \n");
            if(decision == true)
                    {
                    // writes a cookie
                    var expiredays = 7;
                    var exdate=new Date()
                    exdate.setDate(exdate.getDate()+expiredays)
                    document.cookie="PartnerAgreement"+ "=" +escape("Agree To Disclaimer")+
                    ((expiredays==null) ? "" : "; expires="+exdate.toGMTString())
                    }
                    else
                    {
                    // redirect
                    window.location = "/_layouts/signout.aspx";

                    // or close the browser window
                    //window.opener='x';
                    //window.close();
                    }
    }

// gets the Cookie if it exists
function GetCookie()
    {
    if (document.cookie.length>0)
            {
            c_name = "PartnerAgreement";
            c_start=document.cookie.indexOf(c_name + "=")
            if (c_start!=-1)
                    {
                    c_start=c_start + c_name.length+1
                    c_end=document.cookie.indexOf(";",c_start)
                    if (c_end==-1) c_end=document.cookie.length
                    return agreement = unescape(document.cookie.substring(c_start,c_end))
                    }
            }
            return "";
    }

1 个答案:

答案 0 :(得分:1)

您的代码说:

((expiredays==null) ? "" : "; expires="+exdate.toGMTString())

0不是null0是“立即”)。将expiredays设为null