"Go" URL encoding (encodeuricomponent and decodeURIComponent)

Source: Internet
Author: User

Transfer from Http://blog.jhonse.com/archives/2032.jhonse

Recently in the use of the CI framework, found a problem, the URL of the Get link, if the Chinese characters, there will be problems, hint: Link characters do not pass, so in the Internet to find a lot of methods, Modify the CI framework's config.php file, as well as the url.php file, have been modified, and finally did not solve the problem.

At the beginning of the use, are in the Google Browser debugging, what problems are not, Chinese characters can pass, at that time also really a little admire Google. However, in IE, 360 browser has been tested, immediately appear above the problem. Think for one hours, the solution is to submit the data, by JS to the Chinese code, and then in the CI framework to receive the encoded data, decoding can be.

Encoding function:

encodeURIComponent ()

The encodeURIComponent () function encodes a string as a URI component.

Grammar
encodeURIComponent (uristring)
Parameters Description
Uristring Necessary. A string that contains the URI component or other text to encode.
return value

A copy of the uristring, where some of the characters will be replaced by a hexadecimal escape sequence.

Description

The method does not encode ASCII letters and numbers, nor does it encode these ASCII punctuation marks: –_. ! ~ * ' ().

Other characters (such as:;/?:@&=+$,# These punctuation marks used to separate the URI component) are replaced by one or more hexadecimal escape sequences.

Hints and Notes

Tip: Note the difference between the encodeURIComponent () function and the encodeURI () function, which assumes that its arguments are part of a URI (such as a protocol, hostname, path, or query string). Therefore, the encodeURIComponent () function escapes punctuation marks that are used to separate the various portions of the URI.

Instance

In this example, we will encode the URI using encodeURIComponent ():

<script type= "Text/javascript" >document.write (encodeURIComponent ("http://www.w3school.com.cn")) document.write ("<br/>") document.write (encodeURIComponent ("http://www.w3school.com.cn/p 1/")) document.write ("<br/>") document.write (encodeURIComponent ("/?:@&=+$#")) </script>

Output:

Http%3a%2f%2fwww.w3school.com.cnhttp%3a%2f%2fwww.w3school.com.cn%2fp%201%2f%2c%2f%3f%3a%40%26%3d%2b%24%23

decoding function:

decodeURIComponent ()

Definition and usage

The decodeURIComponent () function decodes the URI encoded by the encodeURIComponent () function.

Grammar
decodeURIComponent (uristring)
Parameters Description
Uristring Necessary. A string that contains the encoding URI component or other text to decode.
return value

A copy of the uristring, where the hexadecimal escape sequence is replaced by the character they represent.

Instance

In this example, we will use decodeURIComponent () to decode the encoded URI:

<script type= "Text/javascript" >var test1= "Http://www.w3school.com.cn/My first/" document.write ( encodeURIComponent(test1) + "<br /> ") document.write ( decodeURIComponent(test1) ) </script>

Output:

Http%3a%2f%2fwww.w3school.com.cn%2fmy%20first%2fhttp://www.w3school.com.cn/my first/

Reproduced please specify: Jhonse technology Blog-Focus on technical information and technical articles of it blog»

Go URL encoding (encodeuricomponent and decodeURIComponent)

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.