带JavaScript中的示例的encodeURIComponent()和encodeURIComponent()函数

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!!


翻译自: https://www.includehelp.com/code-snippets/encodeURIComponent-and- decodeURIComponent-functions-with-examples-in-javascript.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值