How to use cookies to solve the problem that WeChat localStorage cannot be stored

巴扎黑
Release: 2017-08-11 13:56:33
Original
1797 people have browsed it

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 "" }
Copy after login

Example:

Set cookie , the validity period is 365 days


setCookie('username','123',365);
Copy after login

Retrieval, if the cookie expires, it will return empty


getCookie('username');
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!