Asp. NET development is a simple and effective solution when JavaScript has Chinese characters garbled

Source: Internet
Author: User
Tags setcookie

In general, there is no garbled case when using ASP. JavaScript with Chinese characters, such as: Alert (' Did you see me? In this way, the code to enter Chinese characters directly is not garbled, if it appears, one is to check the Web. config file to ensure the uniform encoding form, and the other is to set the encoding of the page, such as: Charset=utf-8 and other measures, can query the relevant solutions on the Internet,      This is not a question to be elaborated in this essay. This essay mainly discusses, sometimes using JavaScript to call the background code in the incoming Chinese characters will appear garbled, such as access to the cookie contains Chinese characters in the case. I am using asp.net2.0 and the default encoding for the whole site is UTF-8, and there is no other encoding such as GB2312, and the cookie is not defined in the foreground page, but is created in the background. cs file. Here's a straight look at the workaround:
1, the front desk JavaScript key decoding code.
Return unescape (document.cookie.substring ("Starting character ordinal", "ending character ordinal"));
Attached: For information on how to access cookies using JavaScript, please refer to the reproduced content, reprint address: http://www.cnblogs.com/anderslly/archive/2006/10/11/javascriptcookie.html
Problem:
This allows you to use the last settings when you visit a page, or to share data between pages. For example, when a user visits a Web site and sets the size of the page font, they will want to use the same settings for the next visit without having to repeat the settings.
Solution:
When the user browses the page and makes settings, save the settings in a cookie and read the settings in the cookie the next time they visit.
Refer to the following script:


Utility function to retrieve a 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 ancient 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 saved value in the cookie, the name of the cookie entry. Returns an empty string if the cookie entry does not exist.
Use the Setcookie () function to hold the value of the cookie entry, where the first to second two parameters are the name and value of the cookie entry. If you want to set an expiration time for it, then you need to set the third parameter, where you need to get a proper format parameter by Getexpdate ().
Finally, use Deletecookie () to delete an existing cookie entry, in effect, by having the item expire.
The cookie stores the data on the client. The script on the page can read only the cookie value of the domain and server, and if there are multiple servers in the domain, you need to set a fifth parameter to specify the server. Browser capacity is generally limited to 20 name/value pairs per server, each cookie entry not exceeding 4,000 characters, more realistic, a single cookie entry should be less than 2000 characters, that is, do not use cookies on the client to save bulk data.
Different browsers also store cookies differently. IE creates a text file for each domain's cookie, and Netscape stores all cookies in the same text file.
Note: Cookies are stored on the client side, so they can be affected by browser settings, such as users may disable cookies. To detect whether the browser supports cookies, use the attribute navigator.cookieenabled to determine.


Reference: (oreilly) Java Script and Dhtml Cookbook.chm

2. The background of the. cs file to the cookie value when assigning Chinese content code
MyCookie. Values.add ("CookieName", Httputility.urlencodeunicode ("Did you See me?") "));
Attached: The decoding of this section can be reproduced in the following reprint, reprint address: http://www.cnblogs.com/yzxchoice/archive/2006/08/10/473660.html
Decoding:
Httputility.urldecode (request.cookies["flyxnet"). Value

Write-time encoding
response.cookies["Flyxnet"]. Value = Httputility.urlencodeunicode (votemsg);

Note: The contents of the yellow-bottom red characters in the above two points need special attention, and the other type codes are converted to Unicode encoding, which will be decoded by JavaScript's unescape.

Asp. NET development is a simple and effective solution when JavaScript has Chinese characters garbled

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.