写cookie
function setCookie(sName,sValue,expireHours) { var cookieString = sName + "=" + escape(sValue); if (expireHours>0) {//设置过期时间 var date = new Date(); date.setTime(date.getTime() + expireHours * 60 * 1000);//时间单位=毫秒 cookieString = cookieString + "; expire=" + date.toGMTString();// date.toGMTString()转为GMT时间 } document.cookie = cookieString;//写cookie //document.cookie = name + '=' + value + '; path=/; expires=' + ( expires ? date.toGMTString() : '0' ); } setCookie(divPageVoteID,"true",20)//设置过期时间20分钟 //setCookie("visited","true")//不设置过期时间
读cookie
var allcookies = document.cookie; var cookie_pos = allcookies.indexOf(divPageVoteID); // 如果找到了索引,就代表cookie存在, // 反之,就说明不存在。 if(cookie_pos != -1) { $(divVoteID).innerHTML = "谢谢"; }