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>
    

      

  • 相关阅读:
    面试官本拿求素数搞我,但被我用素数筛优雅的“回击“了
    手写玩具
    【LeetCode】5638.吃苹果的最大数目
    【LeetCode】290.单词规律(双映射)
    【LeetCode】42.接雨水
    【LeetCode】84.柱状图中最大的矩形
    【LeetCode】135.分发糖果
    【Leetcode】746.使用最小花费爬楼梯
    【LeetCode】316.去除重复字母
    【LeetCode】三题解决常见异或运算题
  • 原文地址:https://www.cnblogs.com/sumg/p/3787786.html
Copyright © 2011-2022 走看看