首页 > 语言 > JavaScript > 正文

javascript记住用户名和登录密码(两种方式)

2024-05-06 16:24:21
字体:大 中 小
来源:转载
供稿:网友

这篇文章主要通过两种方式介绍javascript记住用户名和登录密码,有需要的小朋友可以来参考下

下面主要通过代码给大家展示下javascript记住用户名和登录密码,具体代码内容请看下文。

第一种方式:

CONTENT login.html welcome.html cookie.js common.js

login.html

 

 
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  2. <html xmlns="http://www.w3.org/1999/xhtml"> 
  3. <head> 
  4. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
  5. <title>login</title> 
  6. <script type="text/javascript" src="cookie.js"></script> 
  7. <script type="text/javascript" src="common.js"></script> 
  8. </head> 
  9. <body> 
  10. <form action=""> 
  11. <p> 
  12. <span>UserName:</span> 
  13. <input id="userName" type="text" value=""/></p> 
  14. <p> 
  15. <span>Password:</span> 
  16. <input id="password" type="password" value=""/></p> 
  17. <p> 
  18. <span style="font-size:12px; color:blue;">记住密码</span> 
  19. <input id="saveCookie" type="checkbox" value="" /></p> 
  20. <p> 
  21. <input id="submit" type="button" value="GO" /> 
  22. </p> 
  23. </form> 
  24. </body> 
  25. </html> 

welcome.html

 

 
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  2. <html xmlns="http://www.w3.org/1999/xhtml"> 
  3. <head> 
  4. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
  5. <title>welcome</title> 
  6. </head> 
  7. <body> 
  8. <h1>Welcome!</h1> 
  9. <a href="login.html">点击返回登陆框</a> 
  10. </body> 
  11. </html> 
  12. cookie.js 
  13. //新建cookie。 
  14. //hours为空字符串时,cookie的生存期至浏览器会话结束。hours为数字0时,建立的是一个失效的cookie,这个cookie会覆盖已经建立过的同名、同path的cookie(如果这个cookie存在)。 
  15. function setCookie(name,value,hours,path){ 
  16. var name = escape(name); 
  17. var value = escape(value); 
  18. var expires = new Date(); 
  19. expires.setTime(expires.getTime() + hours*3600000); 
  20. path = path == "" ? "" : ";path=" + path; 
  21. _expires = (typeof hours) == "string" ? "" : ";expires=" + expires.toUTCString(); 
  22. document.cookie = name + "=" + value + _expires + path; 
  23. } 
  24. //获取cookie值 
  25. function getCookieValue(name){ 
  26. var name = escape(name); 
  27. //读cookie属性,这将返回文档的所有cookie 
  28. var allcookies = document.cookie;  
  29. //查找名为name的cookie的开始位置 
  30. name += "="; 
  31. var pos = allcookies.indexOf(name);  
  32. //如果找到了具有该名字的cookie,那么提取并使用它的值 
  33. if (pos != -1){ //如果pos值为-1则说明搜索"version="失败 
  34. var start = pos + name.length; //cookie值开始的位置 
  35. var end = allcookies.indexOf(";",start); //从cookie值开始的位置起搜索第一个";"的位置,即cookie值结尾的位置 
  36. if (end == -1) end = allcookies.length; //如果end值为-1说明cookie列表里只有一个cookie 
  37. var value = allcookies.substring(start,end); //提取cookie的值 
  38. return (value); //对它解码  
  39. }  
  40. else return ""; //搜索失败,返回空字符串 
  41. } 
  42. //删除cookie 
  43. function deleteCookie(name,path){ 
  44. var name = escape(name); 
  45. var expires = new Date(0); 
  46. path = path == "" ? "" : ";path=" + path; 
  47. document.cookie = name + "="+ ";expires=" + expires.toUTCString() + path; 
  48. } 

common.js

 

 
  1. function $(objStr){return document.getElementByIdx_x_x(objStr);} 
  2. window.onload = function(){ 
  3. //分析cookie值,显示上次的登陆信息 
  4. var userNameValue = getCookieValue("userName"); 
  5. $("userName").value = userNameValue; 
  6. var passwordValue = getCookieValue("password"); 
  7. $("password").value = passwordValue;  
  8. //写入点击事件 
  9. $("submit").onclick = function() 
  10. { 
  11. var userNameValue = $("userName").value; 
  12. var passwordValue = $("password").value; 
  13. //服务器验证(模拟)  
  14. var isAdmin = userNameValue == "admin" && passwordValue =="123456"; 
  15. var isUserA = userNameValue == "userA" && passwordValue =="userA"; 
  16. var isMatched = isAdmin || isUserA; 
  17. if(isMatched){ 
  18. if( $("saveCookie").checked){  
  19. setCookie("userName",$("userName").value,24,"/"); 
  20. setCookie("password",$("password").value,24,"/"); 
  21. }  
  22. alert("登陆成功,欢迎你," + userNameValue + "!"); 
  23. self.location.replace("welcome.html"); 
  24. } 
  25. else alert("用户名或密码错误,请重新输入!");  
  26. } 
  27. } 

第二种方式:

 

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

以上就是用两种方式展示javascript记住用户名和登录密码的全部代码,没有来得及整理运行效果图,希望大家能够喜欢。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选