10Cookie

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <script src="Scripts/jquery-1.8.2.min.js"></script>
    <script src="Scripts/jquery.cookie.js"></script>
    <script type="text/javascript">
        $(function () {
            $("#btnLogin").click(function () {
                var strName = $("#txtName").val();
                var strPwd = $("#txtPwd").val();
                if (strName == "sa" && strPwd == "123") {
                    $.cookie("userName", strName, { expires: 1 });//没有设置失效时间,所以是缓存cookie,有设置失效时间则是硬盘cookie,expires单位是天
                    alert("登陆成功");
                    window.location.href = "11Index.html";

                }
            });
        });
    </script>
</head>
<body>
    用户名:<input type="text" id="txtName" /><br />
    密码:<input type="text" id="txtPwd" /><br />
    <input type="button" id="btnLogin" value="登录" />
</body>
</html>

  登陆后的页面

 

 

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <script src="Scripts/jquery-1.8.2.min.js"></script>
    <script src="Scripts/jquery.cookie.js"></script>
    <script type="text/javascript">
        $(function () {
            var strName = $.cookie("userName");
            if (strName && strName != "")//判断既不为null也不为undefined   和不为空字符串
            {
                $('#userName').text();
            } else {
                alert("请先登录");
                window.location = '10Cookie.html';
            }

        });
        //退出方法
        function exit() {
            $.cookie("userName", "", { expires: -1 });//设置过期
            alert("退出成功");
        }
    </script>
</head>
<body>
    首页,欢迎<span id="userName"></span>
    <a href="javascript:exit()">退出登录</a>
</body>
</html>

  

posted @ 2014-06-14 00:01  编程猴子  阅读(191)  评论(0编辑  收藏  举报