Detailed description of javascript URL encoding and anti-encoding usage

Source: Internet
Author: User

In js, if we want to encode the url, the commonly used functions include escape, encodeURI, and encodeURIComponent. The corresponding three decoding functions are: unscape, decodeURI, and decodeURIComponent, next we will introduce the conversion principles of these functions.

In the past few days, I have used AJAX to create a new page without refreshing the text. For more information, JavaScript code involves three functions:

Escape, encodeURI, encodeURIComponent, corresponding three decoding functions: unescape, decodeURI, decodeURIComponent

Escape () method:

The specified string is encoded using the ISO Latin character set. 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.

Characters not encoded by this method: @ */+

Escape unencoded characters are 69: *, +,-,.,/, @, _, 0-9, a-z, A-Z

EncodeURI () method:

Convert a URI string to an escape string in UTF-8 encoding format.

Characters not encoded by this method :! @ # $ & * () = :/;? +'

EncodeURI is not encoded with 82 characters :!, #, $, &, ', (,), *, +,-,.,/,:,;, = ,?, @,_,~, 0-9, a-z, A-Z

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 :! *()'

EncodeURIComponent has 71 unencoded characters :!, ',(,),*,-,.,_,~, 0-9, a-z, A-Z

Because of this, 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 javascript1.5, and escape is available in javascript1.0.

Example

The Code is as follows: Copy code

<! Doctype html public "-// w3c // dtd xhtml 1.0 transitional // en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "content-type" content = "text/html; charset = UTF-8"/>
<Title> js ajax encodeuricomponent </title>
</Head>

<Body>

Var url = "post. php? Name = "+ encodeuricomponent (names) +" & mo = "+ encodeuricomponent (mos) +" & body = "+ encodeuricomponent (bodys) +" & id = "+ <? = $ Id;?>;

I will not talk about the ajax part. The main reason is that the encodeuricomponent is garbled in the past.
</Body>
</Html>

Php processing method,

The Code is as follows: Copy code

$ Name = iconv ('utf-8', 'gb2312', postget ('name '));

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.