Garbled characters are generated when parameters in the page contain Chinese characters.

Source: Internet
Author: User

Javascript has three string-encoded functions: Escape, encodeuri, and encodeuricomponent. The corresponding decoding functions are Unescape, decodeuri, and decodeuricomponent.

Backend C # Can be decoded using request. querystring and server. urldecode;

1. Escape () function

Definition and usage
The escape () function can encode a string so that the string can be read on all computers.

Syntax
Escape (string)

Parameter description
String is required. The string to be escaped or encoded.

Return Value
A copy of the encoded string. Some characters are replaced with hexadecimal escape sequences.

Description
This method does not encode ASCII letters and numbers or the following ASCII punctuation marks :-_.! ~ *'(). All other characters are replaced by escape sequences.

 

2. encodeuri () function
Definition and usage
The encodeuri () function can encode a string as a URI.

Syntax
Encodeuri (uristring)

Parameter description
Uristring is required. A string containing URI or other text to be encoded.

Return Value
A copy of uristring, some of which will be replaced by the hexadecimal escape sequence.

Description
This method does not encode ASCII letters and numbers, and does not encode these ASCII punctuation marks :-_.! ~ *'().

The purpose of this method is to fully encode the URI. Therefore, the encodeuri () function will not escape the following ASCII punctuation marks with special meanings in the URI :;/? : @ & =+ $ ,#

 

3. encodeuricomponent () function

Definition and usage
The encodeuricomponent () function can encode a string as a URI component.

Syntax
Encodeuricomponent (uristring)

Parameter description
Uristring is required. A string containing the URI component or other text to be encoded.

Return Value
A copy of uristring, some of which will be replaced by the hexadecimal escape sequence.

Description
This method does not encode ASCII letters and numbers, and does not encode these ASCII punctuation marks :-_.! ~ *'().

Other characters (such :;/? : @ & =+ $, # The punctuation marks used to separate URI components) are all replaced by one or more hexadecimal escape sequences.

Tips and comments
Tip: note the differences between the encodeuricomponent () function and the encodeuri () function. The former assumes that its parameters are part of the URI (such as the protocol, host name, path, or query string ). Therefore, the encodeuricomponent () function uses escape characters to separate the punctuation marks of each part of the URI.

4. Conclusion:

By analyzing the three functions, we can know that in addition to ASCII letters, numbers, and specific symbols, escape () can escape and encode all the passed strings, therefore, if you want to encode the URL, it is best not to use this method. Encodeuri () is used to encode the entire Uri, because the valid characters in the URI are not encoded and converted. The encodeuricomponent method is the most commonly used method for encoding a single uricomponent (request parameter). It can escape Chinese and special characters in parameters without affecting the whole URL.

5 examples:

1 escape ()

<SCRIPT type = "text/JavaScript">

Document. Write (escape ("http://www.w3school.com.cn/") + "<br/> ")

Document. Write (escape ("?! = () # % &"))

</SCRIPT> output:

HTTP % 3A // www.w3school.com.cn

% 3f % 21% 3D % 28% 29% 23% 26

2 encodeuri ()

<SCRIPT type = "text/JavaScript">

Document. Write (encodeuri ("http://www.w3school.com.cn/") + "<br/> ")

Document. Write (encodeuri ("http://www.w3school.com.cn/My first /"))

Document. Write (encodeuri (",/? : @ & = + $ #"))

</SCRIPT> output:

Http://www.w3school.com.cn/

Http://www.w3school.com.cn/My%20first/

,/? : @ & =+ $ #

Encode the entire URL, and the specific Identifier of the URL will not be transcoded.

3 encodeuricomponent ()

Example 1:

<SCRIPT type = "text/JavaScript">

Document. Write (encodeuricomponent ("http://www.w3school.com.cn /"))

Document. Write ("<br/> ")

Document. Write (encodeuricomponent ("http://www.w3school.com.cn/P 1 /"))

Document. Write ("<br/> ")

Document. Write (encodeuricomponent (",/? : @ & = + $ #"))

</Script output:

HTTP % 3A % 2f % 2fwww.w3school.com.cn
HTTP % 3A % 2f % 2fwww.w3school.com.cn % 2fp % 201% 2f
% 2C % 2f % 3f % 3A % 40% 26% 3D % 2B % 24% 23
Example 2: <script language = "JavaScript"> document. Write ('

<A href = "http://passport.baidu.com /? Logout & Aid = 7 & U = '+ encodeuricomponent ("http://cang.baidu.com/bruce42") +' "> exit </a> '); </SCRIPT>

Encode the parameters in the URL, because the parameter is also a URL. If it is not encoded, the jump of the whole URL will be affected.

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.