Basics of JS --JS url encoding and decoding (the difference in three ways)

Javascript language functions for encoding, a total of three, the oldest one is the escape (). 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.

escape 和 unescape

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 .

Its specific rules that, in addition ASCII字母, 数字punctuation, " @ * _ + - . /" except for all the other characters are encoded. U0000 between symbols is transferred to the form u00ff% xx, the remaining symbols are transferred in the form of% uxxxx. Corresponding decoding function is unescape ().

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.

  • 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.

For example:
Code:

escape('http://www.baidu.com?name=zhang@xiao@jie&order=1')
结果:"http%3A//www.baidu.com%3Fname%3Dzhang@xiao@jie%26order%3D1"

escape('张')
结果:"%u5F20"

decoding:

unescape("http%3A//www.baidu.com%3Fname%3Dzhang@xiao@jie%26order%3D1")
结果:"http://www.baidu.com?name=zhang@xiao@jie&order=1"

unescape("%u5F20")
结果:"张"

encodeURI 和 decodeURI

encodeURI () is Javascript to function in the real URL-encoded.

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.
It is the function corresponding decoding decodeURI ().

Note: It should be noted that it does not single quotes' code.

For example:
Code:

encodeURI('http://www.baidu.com?name=zhang@xiao@jie&order=1')
结果:"http://www.baidu.com?name=zhang@xiao@jie&order=1"

decoding:

decodeURI("http%3A//www.baidu.com%3Fname%3Dzhang@xiao@jie%26order%3D1")
结果:"http%3A//www.baidu.com%3Fname%3Dzhang@xiao@jie%26order%3D1"

encodeURIComponent 和 decodeURIComponent

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 ().

For example:
Code:

encodeURIComponent('http://www.baidu.com?name=zhang@xiao@jie&order=1')
结果:"http%3A%2F%2Fwww.baidu.com%3Fname%3Dzhang%40xiao%40jie%26order%3D1"

decoding:

decodeURIComponent("http%3A%2F%2Fwww.baidu.com%3Fname%3Dzhang%40xiao%40jie%26order%3D1")
"http://www.baidu.com?name=zhang@xiao@jie&order=1"

from:https://segmentfault.com/a/1190000010735689

Published 449 original articles · won praise 2 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_37769323/article/details/104685551