首页 > 语言 > JavaScript > 正文

JavaScript使用cookie实现记住账号密码功能

2024-05-06 16:18:51
字体:
来源:转载
供稿:网友

这篇文章主要介绍了JavaScript使用cookie实现记住账号密码功能,本文直接给出完整测试代码,需要的朋友可以参考下

很多登录功能上都有个“记住密码”的功能,其实无非就是对cookie的读取。

下面展示这个功能的代码,原作者已无法考究。。。。

测试方法:直接输入账号密码,提交后,刷新页面,再输入同样的账号,就可以显示

  1. <!DOCTYPE HTML> 
  2. <head> 
  3. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
  4. <title>js COOKIE 记住帐号或密码</title> 
  5. <script type="text/javascript"
  6. window.onload=function onLoginLoaded() { 
  7. if (isPostBack == "False") { 
  8. GetLastUser(); 
  9.  
  10. function GetLastUser() { 
  11. var id = "49BAC005-7D5B-4231-8CEA-16939BEACD67";//GUID标识符 
  12. var usr = GetCookie(id); 
  13. if (usr != null) { 
  14. document.getElementById('txtUserName').value = usr; 
  15. else { 
  16. document.getElementById('txtUserName').value = "001"
  17. GetPwdAndChk(); 
  18. //点击登录时触发客户端事件 
  19.  
  20. function SetPwdAndChk() { 
  21. //取用户名 
  22. var usr = document.getElementById('txtUserName').value; 
  23. alert(usr); 
  24. //将最后一个用户信息写入到Cookie 
  25. SetLastUser(usr); 
  26. //如果记住密码选项被选中 
  27. if (document.getElementById('chkRememberPwd').checked == true) { 
  28. //取密码值 
  29. var pwd = document.getElementById('txtPassword').value; 
  30. alert(pwd); 
  31. var expdate = new Date(); 
  32. expdate.setTime(expdate.getTime() + 14 * (24 * 60 * 60 * 1000)); 
  33. //将用户名和密码写入到Cookie 
  34. SetCookie(usr, pwd, expdate); 
  35. else { 
  36. //如果没有选中记住密码,则立即过期 
  37. ResetCookie(); 
  38.  
  39. function SetLastUser(usr) { 
  40. var id = "49BAC005-7D5B-4231-8CEA-16939BEACD67"
  41. var expdate = new Date(); 
  42. //当前时间加上两周的时间 
  43. expdate.setTime(expdate.getTime() + 14 * (24 * 60 * 60 * 1000)); 
  44. SetCookie(id, usr, expdate); 
  45. //用户名失去焦点时调用该方法 
  46.  
  47. function GetPwdAndChk() { 
  48. var usr = document.getElementById('txtUserName').value; 
  49. var pwd = GetCookie(usr); 
  50. if (pwd != null) { 
  51. document.getElementById('chkRememberPwd').checked = true
  52. document.getElementById('txtPassword').value = pwd; 
  53. else { 
  54. document.getElementById('chkRememberPwd').checked = false
  55. document.getElementById('txtPassword').value = ""
  56. //取Cookie的值 
  57.  
  58. function GetCookie(name) { 
  59. var arg = name + "="
  60. var alen = arg.length; 
  61. var clen = document.cookie.length; 
  62. var i = 0; 
  63. while (i < clen) { 
  64. var j = i + alen; 
  65. //alert(j); 
  66. if (document.cookie.substring(i, j) == arg) return getCookieVal(j); 
  67. i = document.cookie.indexOf(" ", i) + 1; 
  68. if (i == 0) break
  69. return null
  70. var isPostBack = "<%= IsPostBack %>"
  71.  
  72. function getCookieVal(offset) { 
  73. var endstr = document.cookie.indexOf(";", offset); 
  74. if (endstr == -1) endstr = document.cookie.length; 
  75. return unescape(document.cookie.substring(offset, endstr)); 
  76. //写入到Cookie 
  77.  
  78. function SetCookie(name, value, expires) { 
  79. var argv = SetCookie.arguments; 
  80. //本例中length = 3 
  81. var argc = SetCookie.arguments.length; 
  82. var expires = (argc > 2) ? argv[2] : null
  83. var path = (argc > 3) ? argv[3] : null
  84. var domain = (argc > 4) ? argv[4] : null
  85. var secure = (argc > 5) ? argv[5] : false
  86. document.cookie = name + "=" + escape(value) + ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + ((path == null) ? "" : ("; path=" + path)) + ((domain == null) ? "" : ("; domain=" + domain)) + ((secure == true) ? "; secure" : ""); 
  87.  
  88. function ResetCookie() { 
  89. var usr = document.getElementById('txtUserName').value; 
  90. var expdate = new Date(); 
  91. SetCookie(usr, null, expdate); 
  92. </script> 
  93. </head> 
  94. <body> 
  95. <form id="form1"
  96. <div> 用户名: 
  97. <input type="text" ID="txtUserName" onblur="GetPwdAndChk()"
  98. <input type="password" ID="txtPassword"
  99. 密码: 
  100. <input type="checkbox" ID="chkRememberPwd" /> 
  101. 记住密码 
  102. <input type="button" OnClick="SetPwdAndChk()" value="进入"/> 
  103. </div> 
  104. </form> 
  105. </body> 
  106. </html> 
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选