Url special character passing parameter solution _ javascript skills

Source: Internet
Author: User
Some symbols cannot be directly transmitted in the URL. If you want to pass these special symbols in the URL, you need to use their encoding. The following table lists some special URL symbols and codes. Hexadecimal value
1. In the + URL, the plus sign indicates space % 2B
2. the space in the space URL can be + or encoded as % 20
3./separate directories and subdirectories % 2F
4 .? Separate the actual URL and parameter % 3F
5.% specify special characters % 25
6. # indicates bookmarks % 23
7. & separator between parameters specified in URL % 26
8. = value of the specified parameter in the URL % 3D


Solution:
If you use str. replace ("-","! ") Will only Replace the first matched character.
Str. replace (/\-/g ,"! "), You can replace all matched characters (g is the global flag ).
Replace ()
The replacement character variables in js are as follows:

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

Other materials... For reference only...

When url is used for parameter transfer, parameters or URL addresses of some Chinese names (or containing special characters) are often transmitted. Conversion errors may occur in background processing. In some transfer pages, GB2312 is used, and UTF8 is used on the receiving page. In this way, the received parameters may be different from the original ones. The URL encoded using the urlEncode function on the server is different from the URL encoded using the encodeURI function of the client java.

Java Coding involves three functions: escape, encodeURI, and encodeURIComponent. The corresponding three decoding functions are: unescape, decodeURI, and decodeURIComponent.


Java encoding method:
Escape () method: uses the ISO Latin character set to encode the specified string. All space characters, punctuation marks, special characters, and other non-ASCII characters will be converted into character encoding in % xx format (xx equals to the hexadecimal encoding of this character in the character set table number ). For example, the space character is encoded as % 20. The unescape method is the opposite. Characters not encoded by this method: @ */+


EncodeURI () method: converts a URI string into an escape string in UTF-8 encoding format. Characters not encoded by this method :! @ # $ & * () = :/;? +'

EncodeURIComponent () method: convert a URI string to an escape string in UTF-8 encoding format. Compared with encodeURI (), this method will encode more characters, such. Therefore, if the string contains several parts of the URI, this method cannot be used for encoding. Otherwise, the URL will display an error after the/character is encoded. Characters not encoded by this method :! *()


Therefore, for a Chinese string, if you do not want to convert the string encoding format to the UTF-8 format (for example, when the charset of the original page and the target page is consistent), you only need to use escape. If your page is GB2312 or another code, and the page that accepts the parameter is UTF-8 code, use encodeURI or encodeURIComponent.

In addition, encodeURI/encodeURIComponent is introduced after java1.5, and escape is available in java1.0.
1. encodeURIComponent is required when passing parameters so that the combined url will not be truncated by special characters such.

For example: <language = "java"> write ('logout ');

2. encodeURI can be used for url redirection.

Example: Location. href = encodeURI ("http://cang.baidu.com/do/s? Word = Baidu & ct = 21 ");

3. You can use escape when js uses data.

For example, search for the history record in the bucket.

4. When encoding unicode values other than 0-, escape outputs % u *** format. In other cases, escape, encodeURI, and encodeURIComponent have the same encoding result.


The most commonly used format is encodeURIComponent, which converts special characters such as Chinese and Korean into UTF-8 url encoding, therefore, if you want to use encodeURIComponent to transmit parameters to the backend, you must use backend decoding to support UTF-8 (the encoding method in form is the same as that on the current page)

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.