文章詳情頁(yè)
cookie解決微信不能存儲(chǔ)localStorage的問(wèn)題
瀏覽:196日期:2022-06-02 10:19:03
在開(kāi)發(fā)基于微信的Web頁(yè)面時(shí),發(fā)現(xiàn)有些機(jī)型不能存儲(chǔ)信息到localStorage中,或者是頁(yè)面一旦關(guān)閉,存儲(chǔ)的信息也失效了。
用cookie來(lái)替代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)過(guò)測(cè)試,完全兼容,沒(méi)有出現(xiàn)問(wèn)題. 有需要的小伙伴可以參考下,謝謝支持!
相關(guān)文章:
1. Vue利用localStorage本地緩存使頁(yè)面刷新驗(yàn)證碼不清零功能的實(shí)現(xiàn)2. vue項(xiàng)目如何監(jiān)聽(tīng)localStorage或sessionStorage的變化3. 如何在JavaScript中使用localStorage詳情4. js本地存儲(chǔ)解決方案——localStorage與userData5. Vue基于localStorage存儲(chǔ)信息代碼實(shí)例6. JavaScript本地儲(chǔ)存:localStorage、sessionStorage、cookie的使用7. vue 使用localstorage實(shí)現(xiàn)面包屑的操作8. Javascript怎樣使用SessionStorage和LocalStorage9. 如何在Vue中使localStorage具有響應(yīng)式(思想實(shí)驗(yàn))10. Vuex localStorage的具體使用
排行榜
