How to Use encodeURIComponent in js coding (asp, php)

Source: Internet
Author: User

Because of the need for a new system, we have re-studied JS scripts. During the new URL encoding, we found that the webpage encoding format has a great impact on JS. Here we will write a little bit.

{Var B = siteUrl + this. actionUrl + (this. type = "all "? "": This. type) + "& SearchWord =" + encodeURIComponent (A). replace (/'/g, "% 27"); $ redirect (B )}}

At the time of production, it is found that the outgoing data packet encoding is all in the UTF-8 format, and after correcting the webpage encoding is also not reflected, finally, the problem can be solved after the ENCODE is modified to DECODE.

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

1. encodeURIComponent is required when passing parameters so that the combined url will not be truncated by special characters such.

For example:

2. encodeURI can be used for url redirection.

Example: Location. href = "/encodeURI" ("http://www.jb51.net/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 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

Related Article

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.