Encodeuri,escape,encodeuricomponent Differences in JS

Source: Internet
Author: User
Tags encode string

JS to encode the text involves 3 functions: Escape,encodeuri,encodeuricomponent, the corresponding 3 decoding function: unescape,decodeuri,decodeuricomponent

1, passing parameters need to use encodeuricomponent, so that the combined URL will not be # and other special characters truncated.

For example:

<script language= "javascript" >document.write (' <a href= ' http://passport.baidu.com/?logout&aid=7& U= ' +encodeuricomponent

("Http://cang.baidu.com/bruce42") + ' "> Exit </a& gt; '); </script>


2, the URL to jump when you can use the whole encodeURI

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

Characters encoded using this method can be deserialized in PHP using the UrlDecode () function


3, JS Use the data can use escape

Escape outputs the%u**** format when encoding Unicode values other than 0-255, and in other cases the Escape,encodeuri,encodeuricomponent encoding results are the same.

Escape does not encode characters with 69: *,+,-,.,/,@,_,0-9,a-z,a-z

encodeURI does not encode 82 characters:!,#,$,&, ', (,), *,+,,,-,.,/,:,;,=,?,@,_,~,0-9,a-z,a-z

encodeURIComponent does not encode 71 characters:!, ', (,), *,-,.,_,~,0-9,a-z,a-z

Enclosed is an introduction to this three function:

Escape method

Encode String objects so that they can be read on all computers,

Escape (charstring)
Required option The Charstring parameter is any String object or literal that you want to encode.

Description
The Escape method returns a string value (in Unicode format) that contains the charstring content. All spaces, punctuation, accent marks, and other non-ASCII characters are replaced with%XX encoding.

where xx equals the hexadecimal number that represents the character. For example, a space returns "%20".

Characters with values greater than 255 are stored in%UXXXX format.

Note The escape method cannot be used to encode a Uniform Resource Identifier (URI). The encoding should use the encodeURI and encodeURIComponent methods.

encodeURI method

Encodes a text string into a valid Uniform Resource Identifier (URI).

encodeURI (uristring)
The required uristring parameter represents an encoded URI.

Description
The encodeURI method returns an encoded URI. If you pass the encoding result to decodeURI, the initial string is returned. The encodeURI method does not encode the following characters: ":",

"/", ";" and "?". Use the encodeURIComponent method to encode these characters.

encodeURIComponent method

Encodes a text string as a valid component of a Uniform Resource Identifier (URI).

encodeURIComponent (encodeduristring)
The required encodeduristring parameter represents an encoded URI component.

Description

The encodeURIComponent method returns an encoded URI. If you pass the encoding result to decodeURIComponent, the initial string is returned. Because encodeURIComponent

Method for all character encodings, note that if the string represents a path, such as/folder1/folder2/default.html, the slash will also be encoded. This way, when the code knot

will be invalid when sent to the Web server as a request. If the string contains more than one URI component, use the encodeURI method to encode it.

Encodeuri,escape,encodeuricomponent Differences in JS

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.