While transferring the data over the network or sometimes while saving data to the database, we need to encode the data. The function escape() is a predefined function in JavaScript, which encodes the given string.
在通过网络传输数据或有时将数据保存到数据库时,我们需要对数据进行编码。 函数escape()是JavaScript中的预定义函数,可对给定的字符串进行编码。
It encodes almost all special characters including spaces.
它几乎编码所有特殊字符,包括空格。
Note: escape() function does not encode some of the special characters like @+-/.*_
注意: escape()函数不会编码某些特殊字符,例如@ +-/。* _
Example:
例:
<html>
<head>
<title>JavaScipt Example</title>
</head>
<body>
<script>
str = "Hello world!";
//Actual string
document.write("Actual string is: " + str);
document.write("<br>");
//encoded string
document.write("Encoded string is: " + escape(str));
document.write("<br>");
//assign another string
str = "email id: [email protected]";
//Actual string
document.write("Actual string is: " + str);
document.write("<br>");
//encoded string
document.write("Encoded string is: " + escape(str));
</script>
</body>
</html>
Output
输出量
Actual string is: Hello world!
Encoded string is: Hello%20world%21
Actual string is: email id: [email protected]
Encoded string is: email%20id%3A%[email protected]
翻译自: https://www.includehelp.com/code-snippets/escape-function-with-example-in-javascript.aspx