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

用户名: 密码: 记住密码
ContractedBlock.gif ExpandedBlockStart.gif 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 >

转载于:https://www.cnblogs.com/TaoLiQ/archive/2011/05/30/2063175.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值