As we have discussed that escape(), unescape() and encodeURI(), decodeURI() are used to encode a decodes the data/URI, these functions do not encode some of the special characters like @+-/.*_.
正如我们已经讨论过的那样,使用scape () , unescape()和encodeURI(),decodeURI()来对数据/ URI进行解码,这些函数不会对某些特殊字符进行编码,例如@ +-/。* _ 。
But, the functions encodeURIComponent() and decodeURIComponent() can do it.
但是, 函数encodeURIComponent()和decodeURIComponent()可以做到这一点。
Functions encodeURIComponent() and decodeURIComponent() are used to encode and decode the URI along with @+-/.*_ characters. These functions encode, decode all special characters.
函数encodeURIComponent()和decodeURIComponent()函数用于对URI和@ +-/。* _字符进行编码和解码。 这些功能对所有特殊字符进行编码,解码。
Example:
例:
<html>
<head>
<title>JavaScipt Example</title>
</head>
<body>
<script>
var uri ="test page.aspx?val=Hello world!!";
//using encodeURI() and decodeURI() functions
var enc_str = encodeURI(uri);
var dec_str = decodeURI(uri);
//printing the values
document.write("<b>Using encodeURI() and decodeURI()...</b><br>");
document.write("Actual URI: " + uri);
document.write("<br>");
document.write("Encoded URI: " + enc_str);
document.write("<br>");
document.write("Decoded URI: " + dec_str);
document.write("<br><br>");
//using encodeURIComponent() and decodeURIComponent() functions
var enc_str = encodeURIComponent(uri);
var dec_str = decodeURIComponent(uri);
//printing the values
document.write("<b>Using encodeURIComponent() and decodeURIComponent()...</b><br>");
document.write("Actual URI: " + uri);
document.write("<br>");
document.write("Encoded URI: " + enc_str);
document.write("<br>");
document.write("Decoded URI: " + dec_str);
document.write("<br>");
</script>
</body>
</html>
Output
输出量
Using encodeURI() and decodeURI()...
Actual URI: test page.aspx?val=Hello world!!
Encoded URI: test%20page.aspx?val=Hello%20world!!
Decoded URI: test page.aspx?val=Hello world!!
Using encodeURIComponent() and decodeURIComponent()...
Actual URI: test page.aspx?val=Hello world!!
Encoded URI: test%20page.aspx%3Fval%3DHello%20world!!
Decoded URI: test page.aspx?val=Hello world!!