Methods for encoding and converting JavaScript strings: 1. Use the escape() function to encode strings so that they can be readable on all computers. The syntax is "escape (string)"; 2. Use unescape() function, syntax "unescape(string)".

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
1. escape method
Encode String objects so that they can be readable on all computers,
escape(charString)
Required charstring parameter is to be encoded Any String object or literal.
Description:
The escape method returns a string value (Unicode format) containing the content of charstring. All spaces, punctuation, accents, and other non-ASCII characters are replaced with the %xx encoding, where xx is equal to the hexadecimal number representing the character. For example, spaces are returned as " ". Character values greater than 255 are stored in %uxxxx format.
Note: The escape method cannot be used to encode Uniform Resource Identifiers (URIs). To encode it use the encodeURI and encodeURIComponent methods.
2. unescape method
Decode the String object encoded with the escape method.
unescape(charstring)
Required charstring parameter is the String object to be decoded.
Description:
The unescape method returns a string value containing the contents of charstring. All characters encoded in %xx hexadecimal form are replaced by equivalent characters in the ASCII character set. Characters encoded in the %uxxxx format (Unicode characters) are replaced with Unicode characters with hexadecimal encoding xxxx.
Note: The unescape method cannot be used to decode Uniform Resource Identifiers (URIs). To decode this code, you can use the decodeURI and decodeURIComponent methods
When AJAX uses GET requests to pass Chinese strings, the Chinese strings must also be encoded into unicode. Generally, the built-in function escape() of JS is used. However, it is found A better function to determine the conversion of Chinese characters into unicode encoding is as follows
function uniencode(text)
{
text = escape(text.toString()).replace(/+/g, "%2B");
var matches = text.match(/(%([0-9A-F]{2}))/gi);
if (matches)
{
for (var matchid = 0; matchid < matches.length; matchid++)
{
var code = matches[matchid].substring(1,3);
if (parseInt(code, 16) >= 128)
{
text = text.replace(matches[matchid], '%u00' + code);
}
}
}
text = text.replace('%25', '%u0025');
return text;
}Of course, the server side must perform a second transcoding on the encoded string. Convert the string into UTF-8 encoding .
function convert_int_to_utf8($intval)
{
$intval = intval($intval);
switch ($intval)
{
// 1 byte, 7 bits
case 0:
return chr(0);
case ($intval & 0x7F):
return chr($intval);
// 2 bytes, 11 bits
case ($intval & 0x7FF):
return chr(0xC0 | (($intval >> 6) & 0x1F)) .
chr(0x80 | ($intval & 0x3F));
// 3 bytes, 16 bits
case ($intval & 0xFFFF):
return chr(0xE0 | (($intval >> 12) & 0x0F)) .
chr(0x80 | (($intval >> 6) & 0x3F)) .
chr (0x80 | ($intval & 0x3F));
// 4 bytes, 21 bits
case ($intval & 0x1FFFFF):
return chr(0xF0 | ($intval >> 18)) .
chr(0x80 | (($intval >> 12) & 0x3F)) .
chr(0x80 | (($intval >> 6) & 0x3F)) .
chr(0x80 | ($intval & 0x3F));
}
}【Recommended learning: javascript advanced 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!
React Context API Practical Practice: Optimize multi-state management and component communicationAug 26, 2025 pm 01:24 PMThis tutorial explores in-depth how to use the React Context API to effectively manage multiple related useState states in React applications, especially for scenarios such as filters, to solve the problem of prop drilling between components. The article will demonstrate in detail how to create, provide and consume contexts through two different implementation solutions, thereby realizing decoupling and state sharing between components and improving the maintainability and readability of code.
Implement a method to submit a form within a specific DIV without refreshing the entire pageAug 26, 2025 pm 01:21 PMThis article aims to provide a solution to submit a form to a specific element in the page without refreshing the entire page. We will explore the use of JavaScript to intercept form submission events, send form data to the server through Ajax, and update the results returned by the server to the specified area, thereby achieving the effect of local updates.
Master the logical processing of interactive button state in JavaScriptAug 26, 2025 pm 01:15 PMThis article explores in-depth how to effectively manage complex user interface button status using JavaScript, especially with the "Like/Cancel" feature as an example. We will analyze two main implementation strategies: loop-based iterative state update and functional programming using the array reduce method. Through detailed code examples and logical parsing, the article aims to help developers understand state flow rules and choose the best solution for their scenarios, thus achieving accurate and robust button interaction logic.
Tutorial on implementing front-end page option filtering functionAug 26, 2025 pm 01:09 PMThis article aims to guide developers how to implement a front-end-based option filtering function. We will use an example of school information display to explain in detail how to use JavaScript and CSS to dynamically display and hide page elements, thereby achieving the ability to filter schools by category. This article will cover data structure design, HTML structure construction, JavaScript logic writing, and CSS style setting, etc., to help readers quickly master the implementation methods of front-end filtering functions.
React Keys: Efficiently use Fragment for list rendering in static arraysAug 26, 2025 pm 01:06 PMThis article aims to solve the warning issue caused by the lack of key attribute in React when mapping static arrays to HTML tags. We will dig deep into the principles of React Keys, analyze the misunderstanding of placing keys on elements inside Fragment, and provide solutions to correctly apply keys to React.Fragment, while discussing under what circumstances can be used as keys to optimize list rendering performance and avoid potential errors.
TestCafe: Solve the problem that elements cannot be retrieved in userVariablesAug 26, 2025 pm 01:03 PMThis article aims to solve the problem that when using userVariables configuration in TestCafe, the variable values cannot be correctly obtained due to typos. By analyzing problem code and solutions, help developers avoid similar errors and use the userVariables function correctly.
JavaScript module export name extraction: a simple tutorial to use the AST parserAug 26, 2025 pm 12:57 PMThis article aims to provide an easy way to extract all exported names from the text of a JavaScript ES module. Since it is more complicated to perform lexical analysis directly, this article recommends using ready-made JavaScript parsers (such as Acorn, Esprima or Babel) to parse the code into an abstract syntax tree (AST), and then traverse AST to extract the export information. AST Explorer allows you to easily experiment with different parsers and observe the generated AST structure to better understand and extract the required information.
Use Nuxt 3 to provide user uploaded files: Build API endpointsAug 26, 2025 pm 12:51 PMThis document is intended to resolve the problem that users cannot access directly through the public directory after uploading files in Nuxt 3 applications. We will explore the features of the public directory and provide solutions to build API endpoints to provide these files safely and efficiently.


Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment






