JavaScript code involves three functions: Escape, encodeuri, and encodeuricomponent. The corresponding three decoding functions are: Unescape, decodeuri, and decodeuricomponent.
1,You must use encodeuricomponent when passing parameters so that the combined URL will not be truncated by special characters such.
For example:
<Script language = "JavaScript"> document. Write ('<a href = "http://passport.baidu.com /? Logout & Aid = 7 & U = '+ encodeuricomponent ("http://cang.baidu.com/bruce42") +' "> exit </a & gt; '); </SCRIPT>
2,Encodeuri can be used for URL redirection.
Example: location. href = encodeuri (http://cang.baidu.com/do/s? WORD = China & Ct = 21 );
The urldecode () function can be used to encode characters in PHP.
3,You can use escape when JS uses data.
When encoding Unicode values other than 0-, escape outputs % u *** format. In other cases, escape, encodeuri, and encodeuricomponent have the same encoding result.
Escape unencoded characters are 69: *, +,-,.,/, @, _, 0-9, A-Z, A-Z
Encodeuri is not encoded with 82 characters :!, #, $, &, ', (,), *, +,-,.,/,:,;, = ,?, @,_,~, 0-9, A-Z, A-Z
Encodeuricomponent has 71 unencoded characters :!, ',(,),*,-,.,_,~, 0-9, A-Z, A-Z
The following is an introduction to the three functions:
Escape Method
Encode string objects so that they can be read on all computers,
Escape (charstring)
The required charstring parameter is any string object or text to be encoded.
Description
The escape method returns a string containing the charstring content (in unicode format ). All spaces, punctuation marks, accents, and other non-ASCII characters are replaced by % XX encoding. XX equals to the hexadecimal number of the character. For example, "% 20" is returned by a space ".
The characters with a value greater than 255 are stored in % uxxxx format.
Note:The escape method cannot be used to encode uniform resource identifiers (URIs. Use the encodeuri and encodeuricomponent methods for encoding.
Encodeuri Method
Encodes a text string into a valid Uniform Resource Identifier (URI ).
Encodeuri (uristring)
The required uristring parameter represents an encoded Uri.
Description
The encodeuri method returns an encoded Uri. If you pass the encoding result to decodeuri, the initial string is returned. The encodeuri method does not encode the following characters: ":", "/", ";", and "? ". Use the encodeuricomponent method to encode these characters.
Encodeuricomponent Method
Encodes a text string into a valid component of a uniform resource identifier (URI.
Encodeuricomponent (encodeduristring)
The required encodeduristring parameter represents an encoded URI component.
Description
The encodeuricomponent method returns an encoded Uri. If you pass the encoding result to decodeuricomponent, the initial string is returned. Because the encodeuricomponent method encodes all characters, note that if the string represents a path, for example,/folder1/folder2/default.html, the Slash will also be encoded. In this way, when the encoding result is sent to the Web server as a request, it will be invalid. If the string contains more than one URI component, use the encodeuri Method for encoding.