[Switch] url three js encoding function escape (), encodeURI (), encodeURIComponent () Introduction of url three js encoding function escape (), encodeURI (), encodeURIComponent () Introduction [turn]

url three js encoding function escape (), encodeURI (), encodeURIComponent () Introduction [turn]

About browser function encoded escape (), encodeURI (), encodeURIComponent ()

1、escape()

escape () function is encoded js one of the oldest. Although this function is now deprecated, but due to historical reasons, many places are still using it, it is necessary to start with it talk.

In fact, escape () can not be directly used for URL encoding, its real role is to return the value of a Unicode character encoding. Such as "Spring Festival," the return result is% u6625% u8282, that is concentrated in the Unicode character, "Spring" is the first 6625 (hexadecimal) character, "Day" is the first 8282 (hexadecimal) characters .

E.g:

javascript: escape ( "Spring"); 
// output "% u6625% u8282" 

javascript: escape ( "the Hello Word"); 
// outputs "hello% 20word"

There are two areas that need attention.

First of all , no matter what the original page is encoded, once Javascript code, they all become unicode characters. In other words, the input and output Javascipt function, the default is Unicode character. This also applies to the following two functions.

JavaScript: Escape ( "\ u6625 \ u8282"); 
// outputs "% u6625% u8282" 

 JavaScript: unescape ( "% u6625% u8282"); 
// outputs "Spring Festival" 

 JavaScript: unescape ( "\ u6625 \ u8282") ; 
// outputs "Spring Festival"

Secondly , escape () does not "+" code. But we know that the page when the form is submitted, and if there is space, it will be converted into the + character. When the server processing data, processed into a number of spaces will +. So, be careful when using.

2、encodeURI()

It looks at the entire URL encoded, so in addition to the common symbol for some other symbols that have special meaning in the URL "; /?: @ & = + $, #," Is not encoded. After coding, output symbols that form utf-8, and adds% before each byte.

Note that it does not single quotes' code.

3、encodeURIComponent()

The last function is a Javascript encoding encodeURIComponent (). The difference between the encodeURI () is, for its part of the URL encoded individually, and not for the entire URL encoded.

Therefore, the "; /?: @ & = + $, #," These are not symbols encoded in encodeURI (), the encodeURIComponent () is encoded in all. As for the specific encoding method, the two are the same.

It is the function corresponding decoding decodeURIComponent ().

encodeURIComponent () compared encodeURI () to be more thorough.

E.g:

<html>
<body>

<script type="text/javascript">

var test1="http://www.haorooms.com/My first/";
var nn=encodeURI(test1);
var now=decodeURI(test1);

var test1="http://www.haorooms.com/My first/";
var bb=encodeURIComponent(test1);
var nnow=decodeURIComponent(bb);

</script>

</body>
</html>

The output is:

http://www.haorooms.com/My%20first/
http://www.haorooms.com/My first/
http%3A%2F%2Fwww.haorooms.com%2FMy%20first%2F
http://www.haorooms.com/My first/

to sum up

escape () can not be directly used for URL encoding, its real role is to return the value of a Unicode character encoding. Such as "Spring Festival," the return result is% u6625% u8282,, escape () does not "+" encoding is mainly used for Chinese character coding, it is now deprecated.

encodeURI () is Javascript to function in the real URL-encoded. Encoding the entire url address, but a symbol of the special meaning of "; /?: @ & = + $, #," Is not encoded. Corresponding decoding function is: decodeURI ().

encodeURIComponent () encodes "; /: @ & = + $, #?" These special characters. Corresponding decoding function is decodeURIComponent ().

If the URL to be transmitted with ampersands, so use the encodeURIComponent ()

Guess you like

Origin www.cnblogs.com/qiyebao/p/12457275.html