The difference between encodeURI and encodeuricomponent in JavaScript

Source: Internet
Author: User

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 original string is returned. The encodeURI method does not encode the following characters: ":", "/", ";" and "?". Please encode these characters using the encodeURIComponent method.


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 original string is returned. Because the encodeURIComponent method encodes all characters, note that if the string represents a path, such as/folder1/folder2/default.html, the slash will also be encoded. This will not be valid when the encoding result is sent as a request to the Web server. If the string contains more than one URI component, use the encodeURI method to encode


A comparison of the concepts of encodeURI and encodeURIComponent
encodeuri--This function does not correct ASCII letters and numbers and ASCII punctuation (including-_). ! ~ * ' ()) for encoding, other characters (such as:;/?:@&=+$,# These punctuation marks used to separate URI components) are also not escaped.

encodeuricomponent--This function also does not include ASCII letters and numbers and ASCII punctuation (including-_). ! ~ * ' ()) to encode, other characters (such as:;/?:@&=+$,# These punctuation marks used to separate the URI component) are replaced by an escape sequence of one or more hexadecimal components.

decodeURI and decodeURIComponent are their corresponding decoding functions, no longer said. Let's take a look at the test code

Second, encodeURI, encodeuricomponent test code
1, Test JS code:

The code is as follows Copy Code

<script type= "Text/javascript" >
var tst= "http://your domain name/my dir/?a=http://metsky.com/b&c= Hello";
document.write (tst+ "<br/>");

var A=encodeuri (TST);
var B=decodeuri (a);
document.write (A + "<br/>");
document.write (b + "<br/>");

var c=encodeuricomponent (TST);
var d=decodeuricomponent (c);
document.write (c+ "<br/>");
document.write (d);
</script>

2. Script output:

The code is as follows Copy Code

http://your domain/my dir/?a=http://metsky.com/b&c= ' Hello '

http://your domain/my%20dir/?a=http://metsky.com/b&c= '%e4%bd%a0%e5%a5%bd '

http://your domain/my dir/?a=http://metsky.com/b&c= ' Hello '

http%3a%2f%2f your domain%2fmy%20dir%2f%3fa%3dhttp%3a%2f%2fmetsky.com%2fb%26c%3d '%e4%bd%a0%e5%a5%bd '

http://your domain/my dir/?a=http://metsky.com/b&c= ' Hello '

3, Summary

Pay attention to the red part of the output above, so from the view of the interpretation (Heaven edge oneself understand *_*):

encodeURIComponent is the whole parameter as a component processing, think you give me the next use is "paste" after other "standard" URI to do parameter values, and encodeURI think you let me encode a URI, This URI may be used directly.

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.