Home >Web Front-end >HTML Tutorial >How to use cookies to solve the problem that WeChat localStorage cannot be stored
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
When developing a Web page based on WeChat, it was found that some models cannot store information in localStorage, or once the page is closed, the stored information will not be stored. failed.
Use cookies to replace localStorage to store some simple data. I searched online and found that w3school already has a good solution.
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 , the validity period is 365 days
setCookie('username','123',365);
Retrieval, if the cookie expires, it will return empty
getCookie('username');
The above is the detailed content of How to use cookies to solve the problem that WeChat localStorage cannot be stored. For more information, please follow other related articles on the PHP Chinese website!