Home > Article > Web Front-end > Practical tips for processing URLs with js_javascript tips
However, the latter two convert strings into UTF-8 for transmission, which solves the problem of garbled characters caused by different page encodings.
For example: the encoding format (Charset) of the sending page and the receiving page are inconsistent (assuming that the sending page is GB2312 and the receiving page encoding is UTF-8), using escape() to convert and transmit Chinese strings will cause garbled characters. .
The following are various methods for encoding/decoding URLs under JS:
escape method: The characters @* / A-Z 0-9 a-z will not be encoded, and other non-ASCII characters will be encoded with % XX encoding replacement, decoding uses unescape, the escape method cannot be used to encode "Uniform Resource Identifier" (URI). It should be encoded using the encodeURI and encodeURIComponent methods. encodeURI(): Yes! @ # $ & * ( ) = : / ; ? ' A-Z 0-9 a-z, other characters will be encoded, decode using decodeURI();
If you want to use more The character will be encoded, for example: /.., please use the encodeURIComponent() method. After being encoded by this method, the parameter passed to the server is an invalid character. To decode, use decodeURIComponent()