zoukankan      html  css  js  c++  java
  • 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>
    

      

  • 相关阅读:
    硬件的那些事
    seaJS学习资料参考
    nodejs前端自动化构建
    移动端开发的坑【持续更新...】
    【retina】手机上 1PX 边框
    【面试季之三】IE6兼容问题
    【面试季二】前端性能优化
    【面试季一】若干前端面试题
    【面试的坑】行内元素是否可以设置宽高
    Bootstrap和IE何时能相亲相爱啊~
  • 原文地址:https://www.cnblogs.com/sumg/p/3787786.html
Copyright © 2011-2022 走看看