JS读取cookie(记住账号密码)

用户名: 密码: 记住密码
View Code
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 function GetLastUser() {
7 var id = "49BAC005-7D5B-4231-8CEA-16939BEACD67";//GUID标识符
8 var usr = GetCookie(id);
9 if (usr != null) {
10 document.getElementById('txtUserName').value = usr;
11 } else {
12 document.getElementById('txtUserName').value = "001";
13 }
14 GetPwdAndChk();
15 }
16 //点击登录时触发客户端事件
17
18 function SetPwdAndChk() {
19 //取用户名
20 var usr = document.getElementById('txtUserName').value;
21 alert(usr);
22 //将最后一个用户信息写入到Cookie
23 SetLastUser(usr);
24 //如果记住密码选项被选中
25 if (document.getElementById('chkRememberPwd').checked == true) {
26 //取密码值
27 var pwd = document.getElementById('txtPassword').value;
28 alert(pwd);
29 var expdate = new Date();
30 expdate.setTime(expdate.getTime() + 14 * (24 * 60 * 60 * 1000));
31 //将用户名和密码写入到Cookie
32 SetCookie(usr, pwd, expdate);
33 } else {
34 //如果没有选中记住密码,则立即过期
35 ResetCookie();
36 }
37 }
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
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 } else {
55 document.getElementById('chkRememberPwd').checked = false;
56 document.getElementById('txtPassword').value = "";
57 }
58 }
59 //取Cookie的值
60
61 function GetCookie(name) {
62 var arg = name + "=";
63 var alen = arg.length;
64 var clen = document.cookie.length;
65 //alert(document.cookie);
66 var i = 0;
67 while (i < clen) {
68 var j = i + alen;
69 //alert(j);
70 if (document.cookie.substring(i, j) == arg) return getCookieVal(j);
71 i = document.cookie.indexOf(" ", i) + 1;
72 if (i == 0) break;
73 }
74 return null;
75 }
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
83 function SetCookie(name, value, expires) {
84 var argv = SetCookie.arguments;
85 //本例中length = 3
86 var argc = SetCookie.arguments.length;
87 var expires = (argc > 2) ? argv[2] : null;
88 var path = (argc > 3) ? argv[3] : null;
89 var domain = (argc > 4) ? argv[4] : null;
90 var secure = (argc > 5) ? argv[5] : false;
91 document.cookie = name + "=" + escape(value) + ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + ((path == null) ? "" : ("; path=" + path)) + ((domain == null) ? "" : ("; domain=" + domain)) + ((secure == true) ? "; secure" : "");
92 }
93
94 function ResetCookie() {
95 var usr = document.getElementById('txtUserName').value;
96 var expdate = new Date();
97 SetCookie(usr, null, expdate);
98 }
99 </script>
100 </head>
101 <body>
102 <form id="form1">
103 <div> 用户名:
104 <input type="text" ID="txtUserName" onblur="GetPwdAndChk()">
105 <input type="password" ID="txtPassword">
106 密码:
107 <input type="checkbox" ID="chkRememberPwd" />
108 记住密码
109 <input type="button" OnClick="SetPwdAndChk()" value="进入"/>
110 </div>
111 </form>
112 </body>
113 </html>
posted @ 2011-05-30 12:39  ╰☆Everytime I try to flying  阅读(852)  评论(0编辑  收藏  举报