Home > Web Front-end > JS Tutorial > body text

How to encode and convert javascript strings

青灯夜游
Release: 2023-01-06 11:17:43
Original
4505 people have browsed it

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)
Copy after login

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”
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template