URL special character Transfer parameter resolution method _javascript tips

Source: Internet
Author: User

Hexadecimal value
1. + URL + sign space%2b
2. Spaces in the URL can be used with + or encoding%20
3./delimited directory and subdirectory%2f
4.? Separating the actual URLs and parameters%3f
5.% Specify special character%25
6. # indicates bookmark%23
7. & separator Between the parameters specified in the URL%26
8. = value of the specified parameter in the URL%3d


Way to solve:
Replace () method if directly with Str.replace ("-", "!") Only the first matching character will be replaced.
and Str.replace (/\-/g, "!") You can replace all the matching characters (G is the global flag).
Replace ()
The substitution character variables in JS are as follows:

Data2=data2.replace (/\%/g, "%25");
Data2=data2.replace (/\#/g, "%23");
Data2=data2.replace (/\&/g, "%26");

Some other information ... For reference only ...

When you use a URL for parameter passing, you often pass some parameter or URL address with a Chinese name (or a special character), and a conversion error occurs in the background processing. When you use GB2312 on some delivery pages and use UTF8 on the receiving page, the parameters you receive may be inconsistent with what you originally received. URLs encoded using the server-side UrlEncode function are not the same as URLs encoded using the encodeURI function of client java.

Java encoding text involves 3 functions: Escape,encodeuri,encodeuricomponent, corresponding 3 decoding functions: Unescape,decodeuri,decodeuricomponent


Coding methods in Java:
Escape () Method: encodes the specified string with the ISO Latin character set. All spaces, punctuation, special characters, and other non-ASCII characters will be converted into%XX-formatted character encodings (XX equals the encoded 16 digits of the character in the character set table). For example, spaces's corresponding encoding is%20. The Unescape method is the opposite. Characters that will not be encoded by this method: @ */+


encodeURI () Method: Converts the URI string into a string in escape format using the UTF-8 encoding format. Characters that will not be encoded by this method:! @ # $& * () =:/;? + '

encodeURIComponent () Method: Converts the URI string into a string in escape format using the UTF-8 encoding format. This method encodes more characters than encodeURI (), such as/or characters. So if a string contains several parts of the URI, this method cannot be encoded, otherwise the URL will display an error after the/character is encoded. Characters that will not be encoded by this method:! * ( )


Therefore, for Chinese strings, you only need to use escape if you do not want to convert the string encoding format into a UTF-8 format (such as when the original page and the target page charset are consistent). If your page is GB2312 or other code, and the page that accepts the parameter is UTF-8 encoded, you should use encodeURI or encodeuricomponent.

In addition, Encodeuri/encodeuricomponent was introduced after java1.5, and escape was in the java1.0 version.
1, the transfer of parameters need to use encodeuricomponent, so that the combination of the URL will not be # and other special characters truncated.

For example:< language= "Java" >write (' <a href= ' http://passport.baidu.com/?logout&aid=7&u= ' + encodeURIComponent ("Http://cang.baidu.com/bruce42") + ' "> Exit </a>);</>

2, the URL jump can be used as a whole encodeURI

For example: Location.href=encodeuri ("http://cang.baidu.com/do/s?word= Baidu &ct=21");

3, JS use of data can use escape

For example: History records in the search of Tibet.

4, Escape to encode the Unicode value other than 0-255, output%u**** format, in other cases escape,encodeuri,encodeuricomponent encoding results are the same.


The most used should be encodeuricomponent, which is to convert Chinese, Korean and other special characters into utf-8 format URL encoding, So if you need to use encodeURIComponent when passing parameters to the background, you need background decoding for utf-8 support (the form is encoded in the same way as the current page encoding)

Escape does not encode 69 characters: *,+,-,.

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.