Home > Article > Web Front-end > URL encoding function code in JavaScript_javascript skills
The following is a summary of URL encoding of variable values: It is recommended to use encodeURIComponent(), and both GET and POST methods can be sent.
There are several methods to encode URL strings in JavaScript: escape(), encodeURI(), and encodeURIComponent(). These encodings play different roles.
escape() method:
Encode the specified string using the ISO Latin character set. All spaces, punctuation marks, special characters and other non-ASCII characters will be converted into character encoding in %xx format (xx is equal to the hexadecimal number of the character's encoding in the character set table). For example, the encoding corresponding to the space character is .
Characters that will not be encoded by this method: @ * /
encodeURI() method:
Convert the URI string into escape format using UTF-8 encoding format String.
Characters that will not be encoded by this method: ! @ # $& * ( ) = : / ; ? '
encodeURIComponent() method:
Convert URI string Use UTF-8 encoding format to convert it into a string in escape format. Compared with encodeURI(), this method will encode more characters, such as / and other characters. So if the string contains several parts of the URI, you cannot use this method to encode, otherwise the URL will display an error after the / character is encoded.
Characters that will not be encoded by this method: ! * ( ) '
Therefore, for Chinese strings, if you do not want to convert the string encoding format into UTF-8 format (such as the original page and target When the charset of the page is consistent), you only need to use escape. If your page is GB2312 or other encoding, and the page that accepts parameters is UTF-8 encoded, you must use encodeURI or encodeURIComponent.