首页 > 代码库 > 用cookie实现tips组件的显示或隐藏

用cookie实现tips组件的显示或隐藏

function setCookie(name,value,days){
var exp=new Date(new Date().getTime()+days*24*60*60*1000);
document.cookie=""+name+"="+escape(value)+" ;expires="+exp.toGMTString();
}
function getCookie(name){
var result="";
var myCookie=document.cookie+‘;‘;
var searchName=name+‘=‘;
var startOfCookie=myCookie.indexOf(searchName);
var endOfCookie;
if(startOfCookie!=-1){
startOfCookie+=searchName.length;
endOfCookie=myCookie.indexOf(";",startOfCookie);
result=myCookie.slice(startOfCookie,endOfCookie);
}
return result;
}
(function(){
var oTips=document.getElementById("tips");
var page={
check:function(){
var tips=getCookie(‘tips‘);
if(!tips||tips==‘never_show_again‘){return true;}
if(tips==‘hide‘){return false;}
},
hideTip:function(){

oTips.style.display=‘none‘;
},
showTip:function(){
setCookie(‘tips‘,‘show‘,365);
oTips.style.display=‘inlineBlock‘;
},
init:function(){
var self=this;
if(self.check()){
self.showTip();
}else{
self.hideTip();
}
oTips.onclick=function(){
self.hideTip();
setCookie(‘tips‘,‘hide‘,1);
}
}
}
page.init();
})();

用cookie实现tips组件的显示或隐藏