21 days to learn javascript: Cookie Encoding
This article is excerpted from "21 days learning JavaScript"
Cookies are stored in the cookie file in an unencoded format. However, cookies do not contain special characters such as spaces, semicolons, and commas. If you want to write these special symbols into the cookie, you must encode the cookie with escape before writing the cookie, and then restore it through the Unescape function when reading the cookie.
[Example 14-4] encodes and decodes cookies and tries to add some special characters to cookies. See sample code 14-4.
Sample Code 14-4
01 02 <meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
03 <title> cookie encoding and decoding </title> <! ---Document title->
04 <script language = "JavaScript"> // JavaScript Program
05 function setcookie (name, value) // User-Defined Function
06 {
07 your own Doc ument. Cookie = Name + "=" + escape (value) + ";"; // set cookie
08 alert ("set successfully! ");
09}
10 function getcookie (cookiename, codefind) // User-Defined Function
11 {
12 var cookiestring = Document. Cookie; // obtain the cookie
13 var start = cookiestring. indexof (cookiename + '='); // name of the cookie to be intercepted
14 if (START =-1) // If the cookie with this name does not exist
15 return NULL; // return a null value.
16 start + = cookiename. Length + 1;
17 var end = cookiestring. indexof (';', start); // obtain the cookie value.
18 if (codefind = 1) // execute the if statement when the user views it in decoding mode.
19 {
20 if (END =-1) // prevent ";" colon from being added at last
21 return Unescape (cookiestring. substring (start); // return the encoded value.
22 return Unescape (cookiestring. substring (START, end); // return the encoded value.
23}
24 else
25 {
26 // when the user views the code in non-decoded mode, execute the following three statements:
27 if (END =-1) // prevent ";" colon from being added at last
28 return cookiestring. substring (start); // return the cookie value.
29 return cookiestring. substring (START, end); // return the cookie value.
30}
31}
32 function setvalue () // a custom function
33 {
34 if (name. value! = "") // When the input text is not empty
35 {
36 // when the user's input information is not blank, obtain the input information and call the function to set the cookie
37 setcookie (name. value, value. value );
38 value. value = ""; // clear the text box
39 name. value = ""; // clear the name text box
40}
41 else
42 {
43 // when the variable name entered by the user is null, the system prompts the user to enter incorrect information.
44 alert ("setting failed. The cookie name cannot be blank! ") // Prompt the user failed to set
45}
46}
47 function getvalue (n) // customize a function
48 {
49 If (name. value = "") // The text is empty.
50 {
51 alert ("You have not entered the cookie name to search for"); // check whether the input is empty
52}
53 else
54 {
55 var STR = getcookie (name. Value, n); // when the queried value is not empty, call the queried Function
56 If (STR! = "")
57 {
58 value. value = STR; // obtain the query result
59}
60 else
61 {
62 value. value = "this value is blank! "; // Prompt the customer when the result is null
63}
64}
65}
66 </SCRIPT>
67 68 <body>
69 <label>
70 cookie Name:
71 <input type = "text" name = "name"/> <! -Enter the cookie text box -->
72 </label> <! -Tag end -->
73 <label> <br/> <! -Line feed -->
74 <br/> <! -Line feed -->
75 cookie value:
76 <input type = "text" name = "value"/> <! -Text Box of cookie value -->
77 </label> <! -Tag end -->
78 <p>
79 <label> <! -Set the cookie button -->
80 <input type = "Submit" name = "Submit" value = "set cookie" onclick = "setvalue ()"/>
81 </label>
82 <label> <! -Query the cookie button -->
83 <input type = "Submit" name = "submit2" value = "query cookie" onclick = "getvalue (1)"/>
84 </label>
85 <label> <! -View in non-decoded mode -->
86 <input type = "Submit" name = "submit3" value = "non-decoded query" onclick = "getvalue (0)"/>
87 </label>
88 </P>
89 </body>
[Running result] Open the webpage file running program.
[Code parsing] This code segment is 5th ~ Row 9 is the process of setting cookies. Only one parameter is used here. Code 10th ~ Row 31 is the process of reading cookies. ";" And "=" are used to separate the cookie key and value and locate the desired result.
Warning If a cookie contains special characters, it must be encoded and decoded.