Home  >  Article  >  Web Front-end  >  How to encode and convert javascript strings

How to encode and convert javascript strings

青灯夜游
青灯夜游Original
2021-06-17 16:50:164504browse

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

How to encode and convert javascript strings

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn