Javascript全局函数encodeURIComponent()的使用

最近项目开发中,关于页面跳转或ajax传输数据时,常用到encodeURIComponent全局函数,
目的在于将字符串编码为uri组件进行传递,否则字符串在传递过程中会被解析或者一些特殊字符,/?@:;&=+$#*无法成功接收而导致代码出错。
此函数假定参数URIstring为uri的一部分(比如协议、主机名、路径或查询字符串)
常用:

<script>
document.write(encodeURIComponent("http://www.w3school.com.cn")+ "<br />")
document.write(encodeURIComponent("http://www.w3school.com.cn/My first/")+ "<br />")
document.write(encodeURIComponent(",/?:@&=+$#"))
</script>

另外还有编码相关的全局函数也可使用
encodeURI();
decodeURI();
encodeURIComponent();
decodeURIComponent();

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容