Cookies to solve the problem of micro-letter can not store Localstorage _ experience Exchange

Source: Internet
Author: User
Tags setcookie

When developing a Web page based on micro-trust, it was found that some models could not store information in Localstorage, or that once the page was closed, the stored information was invalidated.

Use cookies instead of Localstorage to store some simple data. Search the Internet, found that W3school has a good solution.

To set Cookies:

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 ()
}

///Retrieve 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 cookies, valid for 365 days

Setcookie (' username ', ' 123 ', 365);

Retrieve, if the cookie fails, will return empty

GetCookie (' username ');

Tested, fully compliant, no problem. There is a need for small partners can refer to, thank you support!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.