首页新闻招聘找找看知识库
  • 回复:3 浏览:6150 2009-12-23 09:34 来自 NineTyNine_LiPei

    两个页面使用同样的方法,另一个页面却不能删除Cookie
    成功的页面是这样的:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head>
    <title>Untitled Page</title>
    <script type="text/javascript">
    function SetCookie(name, value, expires, path, domain, secure) {
    document.cookie
    = name + "=" + encodeURI(value) +
    ((expires)
    ? "; expires=" + expires : "") +
    ((path)
    ? "; path=" + path : "") +
    ((domain)
    ? "; domain=" + domain : "") +
    ((secure)
    ? "; secure" : "");
    }

    function DeleteCookie(name) {
    var date = new Date();
    date.setTime(date.getTime()
    - 10000); //删除一个cookie,就是将其过期时间设定为一个过去的时间
    document.cookie = name + "=删除" + "; expires=" + date.toUTCString();
    //document.cookie = " " + name + "=删除" + "; expires=" + date.toGMTString();
    }

    SetCookie(
    "UserName", "lipei");
    SetCookie(
    "UserPwd", "123123");
    alert(document.cookie);
    DeleteCookie(
    "UserPwd");
    alert(document.cookie);
    </script>
    </head>
    <body>

    </body>
    </html>

     


    不成功的页面是这样的:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head>
    <title></title>
    <script type="text/javascript" src="../JS/jquery-1.3.2.js"></script>
    <script type="text/javascript" src="../JS/WindowHelper.js"></script>
    <script type="text/javascript" src="../JS/CookieHelper.js"></script>
    <script type="text/javascript">
    $(document.body).ready(
    function() {
    var cookieHelper = new CookieHelper();
    var windowHelper = new WindowHelper();
    var userName = windowHelper.QueryString("UserName");
    alert(cookieHelper.ReadCookie(
    "UserName"));
    if (cookieHelper.ReadCookie("UserName") == null) {
    document.URL
    = "../Login.html?ReturnUrl=UserDetail.html";
    }

    //alert(userName);
    $.ajax({
    type:
    "GET",
    cache:
    false,
    url:
    "../Handlers/UserHandler.ashx?Action=GetUserInfo&UserName=" + userName,
    dataType:
    "text",
    processData:
    false,
    error:
    function(error) {
    alert(
    "error:" + error);
    },
    success:
    function(data) {
    var jsonObj = eval("(" + data + ")");
    DataBind(jsonObj);
    }
    });

    $(
    "#btnLogOut").click(function() {
    var cookieHelper = new CookieHelper();
    alert(
    "Cookie:" + document.cookie);
    DeleteCookie(
    "UserName");
    alert(
    "Cookie:" + document.cookie);
    document.URL
    = "../Login.html";
    });
    });
    function DeleteCookie(name) {
    var date = new Date();
    date.setTime(date.getTime()
    - 10000); //删除一个cookie,就是将其过期时间设定为一个过去的时间
    document.cookie = name + "=删除" + "; expires=" + date.toUTCString();
    //document.cookie = " " + name + "=删除" + "; expires=" + date.toGMTString();
    }
    function DataBind(jsonObj) {
    if (jsonObj != null) {
    $(
    "#spanUserName").text(jsonObj.UserName);
    $(
    "#spanQuestion").text(jsonObj.Question);
    $(
    "#spanEmail").text(jsonObj.Email);
    $(
    "#spanImage").text(jsonObj.ImagePath);
    }
    }
    </script>
    </head>
    <body>
    <table border="0" width="80%">
    <tr>
    <td width="30%">用户名:</td>
    <td width="70%"><span id="spanUserName"></span></td>
    </tr>
    <tr>
    <td>问题:</td>
    <td><span id="spanQuestion"></span></td>
    </tr>
    <tr>
    <td>Email:</td>
    <td><span id="spanEmail"></span></td>
    </tr>
    <tr>
    <td>Image:</td>
    <td><span id="spanImage"></span></td>
    </tr>
    <tr>
    <td></td>
    <td></td>
    </tr>
    <tr>
    <td><a href="UserList.html">所有用户列表</a></td>
    <td><input type="button" id="btnLogOut" value="注销" /></td>
    </tr>
    <tr>
    <td></td>
    <td></td>
    </tr>
    </table>
    </body>
    </html>

     

    大家帮我看看这是为什么额?

  • 奇爷
    2014-07-25 14:55 奇爷
    为什么没有评论?
    第1楼 回到顶楼
  • xss392795158
    2014-08-06 15:16 xss392795158
    既然有成功的为什么还要去弄不成功的呢?
    第2楼 回到顶楼
  • zcBB
    2014-12-02 11:25 zcBB
    你可以去看看 date.toUTCString() 和 toGMTString() 的结果有什么差别。
    然后再去找找cookie文件里写的 这个过期时间用的什么格式。
    第3楼 回到顶楼
登录后才能评论,请先登录注册