麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 語言 > 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. //獲取cookie值 
  24. function getCookieValue(name){ 
  25. var name = escape(name); 
  26. //讀cookie屬性,這將返回文檔的所有cookie 
  27. var allcookies = document.cookie;  
  28. //查找名為name的cookie的開始位置 
  29. name += "="
  30. var pos = allcookies.indexOf(name);  
  31. //如果找到了具有該名字的cookie,那么提取并使用它的值 
  32. if (pos != -1){ //如果pos值為-1則說明搜索"version="失敗 
  33. var start = pos + name.length; //cookie值開始的位置 
  34. var end = allcookies.indexOf(";",start); //從cookie值開始的位置起搜索第一個";"的位置,即cookie值結尾的位置 
  35. if (end == -1) end = allcookies.length; //如果end值為-1說明cookie列表里只有一個cookie 
  36. var value = allcookies.substring(start,end); //提取cookie的值 
  37. return (value); //對它解碼  
  38. }  
  39. else return ""//搜索失敗,返回空字符串 
  40. //刪除cookie 
  41. function deleteCookie(name,path){ 
  42. var name = escape(name); 
  43. var expires = new Date(0); 
  44. path = path == "" ? "" : ";path=" + path; 
  45. document.cookie = name + "="";expires=" + expires.toUTCString() + path; 

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. var userNameValue = $("userName").value; 
  11. var passwordValue = $("password").value; 
  12. //服務器驗證(模擬)  
  13. var isAdmin = userNameValue == "admin" && passwordValue =="123456"
  14. var isUserA = userNameValue == "userA" && passwordValue =="userA"
  15. var isMatched = isAdmin || isUserA; 
  16. if(isMatched){ 
  17. if( $("saveCookie").checked){  
  18. setCookie("userName",$("userName").value,24,"/"); 
  19. setCookie("password",$("password").value,24,"/"); 
  20. }  
  21. alert("登陸成功,歡迎你," + userNameValue + "!"); 
  22. self.location.replace("welcome.html"); 
  23. else alert("用戶名或密碼錯誤,請重新輸入!");  

第二種方式:

 

 
  1. <script type="text/javascript"
  2. window.onload=function onLoginLoaded() { 
  3. if(isPostBack == "False") { 
  4. GetLastUser(); 
  5. function GetLastUser() { 
  6. var id = "49BAC005-7D5B-4231-8CEA-16939BEACD67";//GUID標識符 
  7. var usr = GetCookie(id); 
  8. if (usr != null) { 
  9. document.getElementById('txtUserName').value = usr; 
  10. }  
  11. else { 
  12. document.getElementById('txtUserName').value = "001"
  13. GetPwdAndChk(); 
  14. //點擊登錄時觸發客戶端事件 
  15. function SetPwdAndChk() { 
  16. //取用戶名 
  17. var usr = document.getElementById('txtUserName').value; 
  18. alert(usr); 
  19. //將最后一個用戶信息寫入到Cookie 
  20. SetLastUser(usr); 
  21. //如果記住密碼選項被選中 
  22. if(document.getElementById('chkRememberPwd').checked == true) { 
  23. //取密碼值 
  24. var pwd = document.getElementById('txtPassword').value; 
  25. alert(pwd); 
  26. var expdate = new Date(); 
  27. expdate.setTime(expdate.getTime() + 14 * (24 * 60 * 60 * 1000)); 
  28. //將用戶名和密碼寫入到Cookie 
  29. SetCookie(usr, pwd, expdate); 
  30. }  
  31. else { 
  32. //如果沒有選中記住密碼,則立即過期 
  33. ResetCookie(); 
  34. function SetLastUser(usr) { 
  35. var id = "49BAC005-7D5B-4231-8CEA-16939BEACD67"
  36. var expdate = new Date(); 
  37. //當前時間加上兩周的時間 
  38. expdate.setTime(expdate.getTime() + 14 * (24 * 60 * 60 * 1000)); 
  39. SetCookie(id, usr, expdate); 
  40. //用戶名失去焦點時調用該方法 
  41. function GetPwdAndChk() { 
  42. var usr = document.getElementById('txtUserName').value; 
  43. var pwd = GetCookie(usr); 
  44. if (pwd != null) { 
  45. document.getElementById('chkRememberPwd').checked = true
  46. document.getElementById('txtPassword').value = pwd; 
  47. }  
  48. else { 
  49. document.getElementById('chkRememberPwd').checked = false
  50. document.getElementById('txtPassword').value = ""
  51. //取Cookie的值 
  52. function GetCookie(name) { 
  53. var arg = name + "="
  54. var alen = arg.length; 
  55. var clen = document.cookie.length; 
  56. var i = 0; 
  57. while (i < clen) { 
  58. var j = i + alen; 
  59. //alert(j); 
  60. if (document.cookie.substring(i, j) == arg) return getCookieVal(j); 
  61. i = document.cookie.indexOf(" ", i) + 1; 
  62. if (i == 0) break
  63. return null
  64. var isPostBack = "<%= IsPostBack %>"
  65. function getCookieVal(offset) { 
  66. var endstr = document.cookie.indexOf(";", offset); 
  67. if (endstr == -1) endstr = document.cookie.length; 
  68. return unescape(document.cookie.substring(offset, endstr)); 
  69. //寫入到Cookie 
  70. function SetCookie(name, value, expires) { 
  71. var argv = SetCookie.arguments; 
  72. //本例中length = 3 
  73. var argc = SetCookie.arguments.length; 
  74. var expires = (argc > 2) ? argv[2] : null
  75. var path = (argc > 3) ? argv[3] : null
  76. var domain = (argc > 4) ? argv[4] : null
  77. var secure = (argc > 5) ? argv[5] : false
  78. document.cookie = name + "=" + escape(value) + ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + ((path == null) ? "" : ("; path=" + path)) + ((domain == null) ? "" : ("; domain=" + domain)) + ((secure == true) ? "; secure" : ""); 
  79. function ResetCookie() { 
  80. var usr = document.getElementById('txtUserName').value; 
  81. var expdate = new Date(); 
  82. SetCookie(usr, null, expdate); 
  83. </script> 
  84. </head> 
  85. <body> 
  86. <form id="form1"
  87. <div>  
  88. 用戶名:<input type="text" ID="txtUserName" onblur="GetPwdAndChk()"
  89. <input type="password" ID="txtPassword"
  90. 密碼: 
  91. <input type="checkbox" ID="chkRememberPwd" /> 
  92. 記住密碼 
  93. <input type="button" OnClick="SetPwdAndChk()" value="進入"/> 
  94. </div> 
  95. </form> 
  96. </body> 

以上就是用兩種方式展示javascript記住用戶名和登錄密碼的全部代碼,沒有來得及整理運行效果圖,希望大家能夠喜歡。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: av电影院在线观看 | 欧美精品一区二区性色 | 国产日韩一区二区三区在线观看 | 黄色a级片视频 | 国产成人高清成人av片在线看 | 免费观看黄视频 | 久久久久久久.comav | www国产成人免费观看视频,深夜成人网 | 日本搞逼视频 | 黄色片免费视频 | 国产精品无码久久久久 | 国产精品99久久久久久久 | 日本a在线观看 | 黄色视频a级毛片 | 亚洲成人入口 | 亚洲影视中文字幕 | 午夜啪视频| 国产精品999在线 | 色播久久 | 国产1区2| 国产精品一区二区三区99 | 香蕉久久久精品 | 欧美久久一区二区 | 亚洲一区二区免费视频 | 欧美一级片网站 | 黄色片网站免费看 | 欧美成人一区免费视频 | 久草在线综合 | 欧美女孩videos | 中文字幕四区 | 精品国产一区二区三 | 亚洲第一成人在线视频 | www久久综合 | 免费一级毛片电影 | 精品国产乱码久久久久久丨区2区 | 97视频一二区 | 羞羞答答tv | 国产乱一区二区三区视频 | 国产一国产精品一级毛片 | 久久久免费观看完整版 | 久久草草影视免费网 |