When developing a Web page based on WeChat, we found that some models cannot store information in localStorage, or once the page is closed, the stored information becomes invalid. Use cookies to replace localStorage to store some simple data. This article mainly introduces the use of cookies to solve the problem that WeChat cannot store localStorage. Code examples are provided here, friends in need can refer to them, I hope it can help everyone.
Set 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 "" }
Example:
Set cookie, valid for 365 days
setCookie('username','123',365);
Retrieval. If the cookie expires, it will return empty
##
getCookie('username');
HTML5 localStorage knowledge points summary
The difference between the functions cookies, sessionStorage and localStorage in php Department
A brief discussion on local storage of localStorage
The above is the detailed content of Detailed explanation of cookies to solve the problem that WeChat cannot store localStorage. For more information, please follow other related articles on the PHP Chinese website!