cookie解决微信不能存储localStorage的问题
在开发基于微信的Web页面时,发现有些机型不能存储信息到localStorage中,或者是页面一旦关闭,存储的信息也失效了。
用cookie来替代localStorage,存储一些简单的数据。上网查找了一下,发现w3school上已有不错的解决方案。
设置cookie:
functionsetCookie(c_name,value,expiredays)
{
varexdate=newDate()
exdate.setDate(exdate.getDate()+expiredays)
document.cookie=c_name+"="+escape(value)+
((expiredays==null)?"":";expires="+exdate.toGMTString())
}
//取回cookie
functiongetCookie(c_name)
{
if(document.cookie.length>0)
{
c_start=document.cookie.indexOf(c_name+"=")
if(c_start!=-1)
{
c_start=c_start+c_name.length+1
c_end=document.cookie.indexOf(";",c_start)
if(c_end==-1)c_end=document.cookie.length
returnunescape(document.cookie.substring(c_start,c_end))
}
}
return""
}
示例:
设置cookie,有效期为365天
setCookie('username','123',365);
取回,若cookie失效,将返回空
getCookie('username');
经过测试,完全兼容,没有出现问题.有需要的小伙伴可以参考下,谢谢支持!
热门推荐
10 学弟英语高考祝福语简短
11 媳妇生日简短祝福语朋友
12 优美的春节祝福语简短
13 爸爸生日简单祝福语简短
14 中秋祝福语简短有深度
15 叔叔结婚的祝福语简短
16 开工红包祝福语毕业简短
17 喝酒祝福语大全简短新颖
18 除夕祝福语简短创意牛年