Home > Web Front-end > JS Tutorial > Detailed explanation of cookies to solve the problem that WeChat cannot store localStorage

Detailed explanation of cookies to solve the problem that WeChat cannot store localStorage

小云云
Release: 2017-12-26 09:36:13
Original
2990 people have browsed it

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

Example:

Set cookie, valid for 365 days


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

Retrieval. If the cookie expires, it will return empty


##

getCookie('username');
Copy after login
After testing, it is fully compatible. No problem occurred.

Related recommendations:

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!

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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template