//设置cookie的名称,值,过期时间
function setCookie(cookieName,cookieValue,cookieExpire) {
var exDate = new Date();
exDate.setDate(exDate.getDate() + cookieExpire);
document.cookie=cookieName+"="+escape(cookieValue)+((cookieExpire==null)?"":";expires="+exDate.toGMTString());
}
//获取cookie
function getCookie(cookieName) {
if (document.cookie.length > 0) {
cookieStart = document.cookie.indexOf(cookieName + "=");
if (cookieStart != -1) {
cookieStart = cookieStart + cookieName.length + 1;
cookieEnd = document.cookie.indexOf(";", cookieStart);
if (cookieEnd == -1) {
cookieEnd = document.cookie.length;
}
return unescape(document.cookie.substring(cookieStart, cookieEnd));
}
}
return "";
}
//检查cookie
function checkCookie() {
userName = getCookie("userName");
if (userName != null && userName != "") {
alert("Welcome you:" + userName);
}
else {
userName = window.prompt("Please Enter Your Name:", "");
if (userName != null && userName != "") {
setCookie('userName',userName,365);
}
}
}