In JavaScript, you can use the escape() function to perform string encoding conversion. This function uses an escape sequence to replace certain characters to encode a string. The syntax format is "escape(string)".
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
The escape() function encodes a string so that it can be read on all computers.
Syntax:
escape(string)
The escape() method can convert all characters other than ASCII into the escape sequence of %xx or %uxxxx (x represents a hexadecimal number). Unicode characters from \u000 to \u00ff are replaced by the escape sequence %xx, and all other Unicode characters are replaced by the %uxxxx sequence.
Example
var s = "JavaScript 中国"; s = escape(s); console.log(s); //返回字符串“JavaScript%u4E2D%u56FD”
You can use this method to encode the Cookie string to avoid conflicts with other agreed characters, because the punctuation characters contained by Cookie are limited.
Corresponding to the escape() method, the unescape() method can decode the escape() encoded string.
[Related recommendations: javascript learning tutorial]
The above is the detailed content of How to encode and convert javascript strings. For more information, please follow other related articles on the PHP Chinese website!