利用 cookie,实现在html页面 记住我 功能

红太狼 2022-02-20 15:32 638阅读 0赞

场景:

  1. 在平时,我们经常可以在页面中看到记住我的功能,如下图示例,在客户端保存用户输入的登录信息,在我们设定的过期时间内,用户再次访问当前页面时,无需重复输入账号密码信息,方便用户下次进行登录操作,一下例子即为实现该功能记录。

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NpbmF0XzM1NjI2NTU5_size_16_color_FFFFFF_t_70

实现代码:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>页面记住我功能Demo</title>
  6. </head>
  7. <body>
  8. <div style="margin: 18% 43%;text-align: center;">
  9. <p>
  10. <span>账号:</span>
  11. <input type="text" id="account">
  12. </p>
  13. <p>
  14. <span>密码:</span>
  15. <input type="password" id="pwd">
  16. </p>
  17. <p>
  18. <input type="checkbox" id="rememberMe">
  19. <span style="vertical-align: middle">记住我</span>
  20. </p>
  21. <br>
  22. <br>
  23. <p>
  24. <input type="button" id="loginBtn" value="登录">
  25. </p>
  26. </div>
  27. <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
  28. </body>
  29. <script>
  30. $(function () {
  31. initView();
  32. $("#loginBtn").click(function () {
  33. if ($("#rememberMe").is(":checked")) {
  34. setCookie("cookie_account", $("#account").val());
  35. setCookie("cookie_password", $("#pwd").val());
  36. setCookie("rememberMe", true);
  37. } else {
  38. delCookie("cookie_account");
  39. delCookie("cookie_password");
  40. delCookie("rememberMe");
  41. }
  42. window.location.reload()
  43. });
  44. });
  45. function initView() {
  46. if (getCookie("cookie_account")) {
  47. $("#account").val(getCookie("cookie_account"));
  48. }
  49. if (getCookie("cookie_password")) {
  50. $("#pwd").val(getCookie("cookie_password"));
  51. }
  52. if (getCookie("rememberMe")) {
  53. $("#rememberMe").attr("checked", getCookie("rememberMe"));
  54. }
  55. $("#account").focus(function () {
  56. this.select();
  57. });
  58. $("#pwd").focus(function () {
  59. this.select();
  60. });
  61. }
  62. /**
  63. * 写入cookie
  64. * @param name cookie 名
  65. * @param value cookie 值
  66. */
  67. function setCookie(name, value) {
  68. var Days = 30; //此 cookie 将被保存 30 天
  69. var exp = new Date();
  70. exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
  71. document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString();
  72. }
  73. /**
  74. * 删除cookie
  75. * @param name
  76. */
  77. function delCookie(name) {
  78. var exp = new Date();
  79. exp.setTime(exp.getTime() - 1);
  80. var cval = getCookie(name);
  81. if (cval != null) document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
  82. }
  83. /**
  84. * 读取cookie
  85. * @param name
  86. * @returns
  87. */
  88. function getCookie(name) {
  89. var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
  90. if (arr != null)
  91. return unescape(arr[2]);
  92. return null;
  93. }
  94. </script>
  95. </html>

发表评论

表情:
评论列表 (有 0 条评论,638人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Shiro 实现记住功能

    登录一些网站的时候,在登录输入框的下侧一般都会有一个“记住我”的勾选框,选择之后,下次进入网站时就会自动进行登录操作,无需我们再次输入密码。   有关“记住我”的实现原理