The content of this article is about how JavaScript implements URL transcoding and decoding. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
1. escape and unescape
escape() cannot be used directly for URL encoding. Its real function is to return the Unicode encoding value of a character.
Use the unicode character set to encode the specified string except 0-255. All spaces, punctuation marks, special characters and more related non-ASCII characters will be converted into character encoding in %xx format (xx is equal to the hexadecimal number of the character encoding in the character set table). For example, the encoding corresponding to the space character is .
Escape does not encode 69 characters: *, , -, ., /, @, _, 0-9, a-z, A-Z.
The escape() function is used to encode strings in js and is not commonly used.
Encoding:
escape('http://www.baidu.com?name=zhang@xiao@jie&order=1')
Result: "http://www.baidu.com?name=zhang@xiao@jie&order=1"
escape('Zhang')
Result: "%u5F20"
Decoding:
unescape("http%3A//www.baidu.com%3Fname%3Dzhang@xiao@jie%26order%3D1")
Result: "http://www.baidu.com?name=zhang@xiao@jie&order=1"
unescape("%u5F20")
Result: "Zhang"
2. encodeURI and decodeURI
encode the URI string in UTF-8 The format is converted into escape various strings.
encodeURI does not encode 82 characters: !, #, $, &, ', (,), *, ,,, -,.,/,:,;,=,?,@,_,~, 0-9, a-z, A-Z
encodeURI() is used to encode the entire url
Encoding:
encodeURI('http://www.baidu.com?name=zhang@xiao@jie&order=1')
Result: "http://www.baidu.com?name =zhang@xiao@jie&order=1"
Decoding:
decodeURI("http%3A//www.baidu.com%3Fname%3Dzhang@xiao@jie%26order%3D1")
Result: "http://www.baidu.com?name=zhang@xiao@jie&order=1"
3. encodeURIComponent and decodeURIComponent
The difference from encodeURI() is that it is used to encode individual components of the URL rather than encoding the entire URL.
therefore,"; / ? : @ & = $ , #", these symbols that are not encoded in encodeURI() will all be encoded in encodeURIComponent(). As for the specific encoding method, both are the same. Convert the URI string into escape format using UTF-8 encoding format String.
encodeURIComponent() is used to pass parameters. Parameters containing special characters may cause discontinuities.
Encoding:
encodeURIComponent('http://www.baidu.com?name=zhang@xiao@jie&order=1')
Result: "http:// www.baidu.com?name=zhang@xiao@jie&order=1"
Decoding:
decodeURIComponent("http%3A%2F%2Fwww.baidu.com%3Fname%3Dzhang%40xiao%40jie%26order%3D1")
Result:"http://www.baidu.com?name=zhang @xiao@jie&order=1"
Summary:
escape() cannot be used directly for URL encoding. Its real function is to return the Unicode encoding value of a character. For example, the return of "Spring Festival" The result is %u6625%u8282,, escape() is incorrect" "Encoding
is mainly used for Chinese character encoding, and its use is no longer recommended.
encodeURI() is actually used to encode URLs in Javascript Encoding function.
Encodes the entire URL address, but does not encode the symbols with special meanings "; / ? : @ & = $ , #". The corresponding decoding function is: decodeURI().
encodeURIComponent()
Can encode the special characters "; / ? : @ & = $ , #". The corresponding decoding function is decodeURIComponent().
I want to pass the URL with ampersand , so use encodeURIComponent()
The above is the detailed content of How does javascript implement URL transcoding and decoding?. For more information, please follow other related articles on the PHP Chinese website!