ASP. NET is a simple and effective solution when JavaScript contains Chinese characters and garbled characters.

Source: Internet
Author: User
Tags setcookie

Generally, when using ASP. net for development and using JavaScript with Chinese characters, there will be no garbled characters, such as: Alert ('Do you see me? '); In this way, the code for directly Inputting Chinese characters does not contain garbled characters. If so, check the web. the Config File ensures the unified encoding format; the second is to set the page encoding, such as charset = UTF-8 and other measures. You can query related solutions online, which is not a problem described in this article.

In this article, we will mainly discuss that sometimes garbled characters may occur when Javascript is used to call the Chinese characters passed in the background code, for example, when accessing cookies that contain Chinese characters. ASP is used here. net2.0 and the default code for the whole site is UTF-8, and no other encoding formats such as gb2312, And the cookie is not defined in the foreground page value assignment, but in the background. created in the CS file. The solution is as follows:

1. Key decoding code in front-end JavaScript.

Return Unescape (document. Cookie. substring ([Starting character serial number], [ending character serial number ]));

For more information about how to use JavaScript to access cookies, see repost address: http://www.cnblogs.com/anderslly/archive/2006/10/11/javascriptcookie.html

Problem:
This allows you to use the previous settings when accessing the page or share data between different pages. For example, if you set the page font size when visiting a website, you will want to use the same settings for browsing next time without repeated settings.
Solution:
When you browse the page and make settings, save these settings in the cookie and read the settings in the cookie for the next visit.
Refer to the following script:

// Utility function to retrieve an expiration data in proper format;
Function getexpdate (days, hours, minutes)
{
VaR expdate = new date ();
If (typeof (days) = "Number" & typeof (hours) = "Number" & typeof (hours) = "Number ")
{
Expdate. setdate (expdate. getdate () + parseint (days ));
Expdate. sethours (expdate. gethours () + parseint (hours ));
Expdate. setminutes (expdate. getminutes () + parseint (minutes ));
Return expdate. togmtstring ();
}
}

// Utility function called by getcookie ()
Function getcookieval (offset)
{
VaR endstr = Document. Cookie. indexof (";", offset );
If (endstr =-1)
{
Endstr = Document. Cookie. length;
}
Return Unescape (document. Cookie. substring (offset, endstr ));
}

// Primary function to retrieve cookie by name
Function getcookie (name)
{
VaR Arg = Name + "= ";
VaR Alen = Arg. length;
VaR clen = Document. Cookie. length;
VaR I = 0;
While (I <clen)
{
VaR J = I + Alen;
If (document. Cookie. substring (I, j) = Arg)
{
Return getcookieval (j );
}
I = Document. Cookie. indexof ("", I) + 1;
If (I = 0) break;
}
Return;
}

// Store Cookie value with optional details as needed
Function setcookie (name, value, expires, path, domain, secure)
{
Document. Cookie = Name + "=" + escape (value) +
(Expires )? "; Expires =" + expires: "") +
(PATH )? "; Path =" + path: "") +
(Domain )? "; Domain =" + domain: "") +
(Secure )? "; Secure ":"");
}

// Remove the cookie by setting outgoing ent expiration date
Function deletecookie (name, path, domain)
{
If (getcookie (name ))
{
Document. Cookie = Name + "=" +
(PATH )? "; Path =" + path: "") +
(Domain )? "; Domain =" + domain: "") +
"; Expires = Thu, 01-Jan-70 00:00:01 GMT ";
}
}

Use the getcookie (name) function to read the value saved in the cookie. The parameter name is the name of the cookie item. If the cookie does not exist, an empty string is returned.
Use the setcookie () function to save the value of the cookie item. The first and second parameters are the name and value of the cookie item respectively. If you want to set an expiration time for it, you need to set the third parameter. Here, you need to get a correct format parameter through getexpdate.
Finally, you can use deletecookie () to delete an existing cookie by actually making it expire.
Cookie stores data on the client. The page script can only read the cookie values of the domain and server. If there are multiple servers in the domain, you need to set the fifth parameter to specify the server. The browser capacity is generally limited to 20 name/value pairs per server. Each cookie item cannot exceed 4000 characters. More realistically, a single cookie item should be less than 2000 characters, in other words, do not use cookies to store large volumes of data on the client.
Different browsers use different methods to save cookies. IE creates a text file for the cookies in each domain, while Netscape stores all cookies in the same text file.
Note: The cookie is stored on the client, so it is affected by browser settings. For example, the user may disable the cookie. To check whether the browser supports cookies, use the property navigator. cookieenabled to determine whether the browser supports cookies.

Reference: (oreilly) Java Script and DHTML cookbook. CHM

 

2. Code used when the. CS file in the background assigns the cookie value to the Chinese content

Mycookie. Values. Add ("cookiename", httputility. urlencodeunicode ("Have you seen me? "));

Appendix: For the decoding of this part, refer to the following reprint, reprint address: http://www.cnblogs.com/yzxchoice/archive/2006/08/10/473660.html

Decoding:

Httputility. urldecode (request. Cookies ["flyxnet"]. Value

Write Encoding

Response. Cookies ["flyxnet"]. value = httputility. urlencodeunicode (votemsg );

Note: The content of the yellow and red characters in the above two points should be paid special attention. Other types of encoding are converted to unicode encoding, which will be decoded by the Javascript Unescape.

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.