文章詳情頁
cookie解決微信不能存儲(chǔ)localStorage的問題
瀏覽:204日期:2022-06-02 10:19:03
在開發(fā)基于微信的Web頁面時(shí),發(fā)現(xiàn)有些機(jī)型不能存儲(chǔ)信息到localStorage中,或者是頁面一旦關(guān)閉,存儲(chǔ)的信息也失效了。
用cookie來替代localStorage,存儲(chǔ)一些簡(jiǎn)單的數(shù)據(jù)。上網(wǎng)查找了一下,發(fā)現(xiàn)w3school上已有不錯(cuò)的解決方案。
設(shè)置cookie:
function setCookie(c_name,value,expiredays) { var exdate=new Date() exdate.setDate(exdate.getDate()+expiredays) document.cookie=c_name+ "=" +escape(value)+ ((expiredays==null) ? "" : ";expires="+exdate.toGMTString()) } //取回cookie function getCookie(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 return unescape(document.cookie.substring(c_start,c_end)) } } return "" }
示例:
設(shè)置cookie,有效期為365天
setCookie("username","123",365);
取回,若cookie失效,將返回空
getCookie("username");
經(jīng)過測(cè)試,完全兼容,沒有出現(xiàn)問題. 有需要的小伙伴可以參考下,謝謝支持!
相關(guān)文章:
1. vue 使用localstorage實(shí)現(xiàn)面包屑的操作2. js本地存儲(chǔ)解決方案——localStorage與userData3. vue項(xiàng)目如何監(jiān)聽localStorage或sessionStorage的變化4. Vue基于localStorage存儲(chǔ)信息代碼實(shí)例5. JavaScript本地儲(chǔ)存:localStorage、sessionStorage、cookie的使用6. Vuex localStorage的具體使用7. Javascript怎樣使用SessionStorage和LocalStorage8. 如何在JavaScript中使用localStorage詳情9. Vue利用localStorage本地緩存使頁面刷新驗(yàn)證碼不清零功能的實(shí)現(xiàn)10. 如何在Vue中使localStorage具有響應(yīng)式(思想實(shí)驗(yàn))
排行榜
