var onOff
;
$
(function() {
GetLastUser();
$
('.denglu-btn').click(function() {
loginEvent()
})
document
.onkeydown
= function(event
) {
var e
= event
|| window
.event
|| arguments
.callee
.caller
.arguments
[0];
if(e
&& e
.keyCode
== 13) {
loginEvent()
}
};
})
function
loginEvent() {
SetPwdAndChk();
}
function
GetLastUser() {
var id
= "49BAC005-7D5B-4231-8CEA-16939BEACD67";
var usr
= GetCookie(id
);
if(usr
!= null
) {
document
.getElementById('username').value
= usr
;
} else {
document
.getElementById('username').value
= "";
}
GetPwdAndChk();
}
function
SetPwdAndChk() {
var usr
= document
.getElementById('username').value
;
SetLastUser(usr
);
if(document
.getElementById('chkRememberPwd').checked
== true) {
var pwd
= Base64
.encode(document
.getElementById('password').value
);
var expdate
= new Date();
expdate
.setTime(expdate
.getTime() + 14 * (24 * 60 * 60 * 1000));
SetCookie(usr
, pwd
, expdate
);
} else {
ResetCookie();
}
}
function
SetLastUser(usr
) {
var id
= "49BAC005-7D5B-4231-8CEA-16939BEACD67";
var expdate
= new Date();
expdate
.setTime(expdate
.getTime() + 14 * (24 * 60 * 60 * 1000));
SetCookie(id
, usr
, expdate
);
}
function
GetPwdAndChk() {
var usr
= document
.getElementById('username').value
;
if(GetCookie(usr
)){
var pwd
= Base64
.decode(GetCookie(usr
));
}else{
var pwd
= null
;
}
if(pwd
!= null
) {
document
.getElementById('chkRememberPwd').checked
= true;
document
.getElementById('password').value
= pwd
;
} else {
document
.getElementById('chkRememberPwd').checked
= false;
document
.getElementById('password').value
= "";
}
}
function
GetCookie(name
) {
var arg
= name
+ "=";
var alen
= arg
.length
;
var clen
= document
.cookie
.length
;
var i
= 0;
while(i
< clen
) {
var j
= i
+ alen
;
if(document
.cookie
.substring(i
, j
) == arg
) return getCookieVal(j
);
i
= document
.cookie
.indexOf(" ", i
) + 1;
if(i
== 0) break;
}
return null
;
}
function
getCookieVal(offset
) {
var endstr
= document
.cookie
.indexOf(";", offset
);
if(endstr
== -1) endstr
= document
.cookie
.length
;
return unescape(document
.cookie
.substring(offset
, endstr
));
}
function
SetCookie(name
, value
, expires
) {
var argv
= SetCookie
.arguments
;
var argc
= SetCookie
.arguments
.length
;
var expires
= (argc
> 2) ? argv
[2] : null
;
var path
= (argc
> 3) ? argv
[3] : null
;
var domain
= (argc
> 4) ? argv
[4] : null
;
var secure
= (argc
> 5) ? argv
[5] : false;
document
.cookie
= name
+ "=" + escape(value
) + ((expires
== null
) ? "" : ("; expires=" + expires
.toGMTString())) + ((path
== null
) ? "" : ("; path=" + path
)) + ((domain
== null
) ? "" : ("; domain=" + domain
)) + ((secure
== true) ? "; secure" : "");
}
function
ResetCookie() {
var usr
= document
.getElementById('username').value
;
var expdate
= new Date();
SetCookie(usr
, null
, expdate
);
}
document.cookie
1.获取所有cookie
allCookies = document.cookie; 在上面的代码中,allCookies被赋值为一个字符串,该字符串包含所有的Cookie,每条cookie以"分号和空格(; )"分隔(即, key=value 键值对)。
2.设置cookie
document.cookie = newCookie; newCookie 是一个键值对形式的字符串。 需要注意的是,用这个方法一次只能对一个cookie进行设置或更新。
以下可选的cookie属性值可以跟在键值对后,用来具体化对cookie的设定/更新,使用分号以作分隔:
;path=path (例如 ‘/’, ‘/mydir’) 如果没有定义,默认为当前文档位置的路径。;domain=domain (例如 ‘example.com’, ‘subdomain.example.com’) 如果没有定义,默认为当前文档位置的路径的域名部分。与早期规范相反的是,在域名前面加 . 符将会被忽视,因为浏览器也许会拒绝设置这样的cookie。如果指定了一个域,那么子域也包含在内。;max-age=max-age-in-seconds (例如一年为606024*365);expires=date-in-GMTString-format 如果没有定义,cookie会在对话结束时过期 这个值的格式参见Date.toUTCString();secure (cookie只通过https协议传输)
cookie的值字符串可以用encodeURIComponent()来保证它不包含任何逗号、分号或空格(cookie值中禁止使用这些值).
所以,在你更新cookie或者删除cookie的时候, 要注意与添加cookie时用同样的域和路径,不同的域、不同的路径下可以存在同样名字的cookie,
封装cookie的设置及清除方法
(function($
) {
var util
= {};
$
.extend(util
, {
cookie
: function(name
, value
, options
) {
if (typeof value
!= 'undefined') {
options
= options
|| {};
if (value
=== null) {
value
= '';
options
.expires
= -1;
}
var expires
= '';
if (options
.expires
&& (typeof options
.expires
== 'number' || options
.expires
.toUTCString
)) {
var date
;
if (typeof options
.expires
== 'number') {
date
= new Date();
date
.setTime(date
.getTime() + (options
.expires
* 24 * 60 * 60 * 1000));
} else {
date
= options
.expires
;
}
expires
= '; expires=' + date
.toUTCString();
}
var path
= options
.path
? '; path=' + (options
.path
) : '';
var domain
= options
.domain
? '; domain=' + (options
.domain
) : '';
var secure
= options
.secure
? '; secure' : '';
document
.cookie
= [name
, '=', encodeURIComponent(value
), expires
, path
, domain
, secure
].join('');
} else {
var cookieValue
= null;
if (document
.cookie
) {
var cookies
= document
.cookie
.split(';');
for (var i
= 0; i
< cookies
.length
; i
++) {
var cookie
= $
.trim(cookies
[i
]);
if (cookie
.substring(0, name
.length
+ 1) == (name
+ '=')) {
cookieValue
= decodeURIComponent(cookie
.substring(name
.length
+ 1));
break;
}
}
}
return cookieValue
;
}
},
removeAllCookie
: function(options
) {
var cookies
= document
.cookie
.split(";");
for (var i
= 0; i
< cookies
.length
; i
++) {
if (cookies
[i
].indexOf(" ") == 0) {
cookies
[i
] = cookies
[i
].substring(1);
}
var cookie_name
= cookies
[i
].split("=")[0];
util
.cookie(cookie_name
,null,options
)
}
}
});
window
.util
= util
;
})(jQuery
);