How to solve the garbled characters when JavaScript reads Chinese cookies _ javascript skills

Source: Internet
Author: User
Garbled characters occur when reading Chinese cookies. The following is a specific solution. Do not use Chinese characters in the future. The Code is as follows:


Function Get_Cookie (name ){
Var start = document. cookie. indexOf (name +... = ...);
Var len = start + name. length + 1;
If ((! Start) & (name! = Document. cookie. substring (0, name. length) return null;
If (start =-1) return null;
Var end = document. cookie. indexOf (...;..., Len );
If (end =-1) end = document. cookie. length;
Return unescape (document. cookie. substring (len, end ));
}

Function Set_Cookie (name, value, expires, path, domain, secure ){
Expires = expires * 60*60*24*1000;
Var today = new Date ();
Var expires_date = new Date (today. getTime () + (expires ));
Var cookieString = name +... =... + Escape (value) +
(Expires )? ...; Expires =... + Expires_date.toGMTString ():......) +
(Path )? ...; Path =... + Path :......) +
(Domain )? ...; Domain =... + Domain :......) +
(Secure )? ...; Secure... :......);
Document. cookie = cookieString;
}


When Chinese information is stored in cookies, garbled characters will occur, which will solve the problem after modification.

The Code is as follows:


// Cookie operation function
Function Get_Cookie (name ){
Var start = document. cookie. indexOf (name +... = ...);
Var len = start + name. length + 1;
If ((! Start) & (name! = Document. cookie. substring (0, name. length) return null;
If (start =-1) return null;
Var end = document. cookie. indexOf (...;..., Len );
If (end =-1) end = document. cookie. length;
Return decodeURI (document. cookie. substring (len, end ));
}

Function Set_Cookie (name, value, expires, path, domain, secure ){
Expires = expires * 60*60*24*1000;
Var today = new Date ();
Var expires_date = new Date (today. getTime () + (expires ));
Var cookieString = name +... =... + Escape (value) +
(Expires )? ...; Expires =... + Expires_date.toGMTString ():......) +
(Path )? ...; Path =... + Path :......) +
(Domain )? ...; Domain =... + Domain :......) +
(Secure )? ...; Secure... :......);
Document. cookie = cookieString;
}


It mainly changed unescape to decodeURI.
Related Article

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.