js中escape,encodeURI,encodeURIComponent

js对文字进行编码涉及3个函数:escape,encodeURI,encodeURIComponent,相应3个解码函数:unescape,decodeURI,decodeURIComponent

1、   传递参数时需要使用encodeURIComponent,这样组合的url才不会被#等特殊字符截断。                            

例如:<script language="javascript">document.write('<a href="http://passport.baidu.com/?logout&aid=7&u='+encodeURIComponent("http://cang.baidu.com/bruce42")+'">退出</a>');</script>

2、   进行url跳转时可以整体使用encodeURI

例如:Location.href=encodeURI("http://cang.baidu.com/do/s?word=百度&ct=21");

3、   js使用数据时可以使用escape

[Huoho.Com编辑]

例如:搜藏中history纪录。

4、   escape对0-255以外的unicode值进行编码时输出%u****格式,其它情况下escape,encodeURI,encodeURIComponent编码结果相同。


最多使用的应为encodeURIComponent,它是将中文、韩文等特殊字符转换成utf-8格式的url编码,所以如果给后台传递参数需要使用encodeURIComponent时需要后台解码对utf-8支持(form中的编码方式和当前页面编码方式相同)

escape不编码字符有69个:*,+,-,.,/,@,_,0-9,a-z,A-Z

encodeURI不编码字符有82个:!,#,$,&,',(,),*,+,,,-,.,/,:,;,=,?,@,_,~,0-9,a-z,A-Z

encodeURIComponent不编码字符有71个:!, ',(,),*,-,.,_,~,0-9,a-z,A-Z

 

javaScript中URL编码转换,escape() encodeURI() encodeURIComponent
2007年05月12日 星期六 下午 04:48

 在使用url进行参数传递时,经常会传递一些中文名的参数或URL地址,在后台处理时会发生转换错误。在有些传递页面使用GB2312,而在接收页面使用UTF8,这样接收到的参数就可能会与原来发生不一致。使用服务器端的urlEncode函数编码的URL,与使用客户端javascript的encodeURI函数编码的URL,结果就不一样。

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
JavaScriptencodeURI()函数用于将URI字符串转换为UTF-8编码格式的escape格式字符串。这个函数会编码除了以下字符之外的所有字符:! @ # $ & ( ) = : / ; ? + '。\[1\]例如,如果你想编码一个带有特殊字符(比如文)的URI,你可以使用encodeURI()函数。例如,如果原始URI是"http://www.365mini.com/page/tag/国近代史的前车之鉴",那么使用encodeURI()函数后,编码后的URI将变为"http://www.365mini.com/page/tag/%E4%B8%AD%E5%9B%BD%E8%BF%91%E4%BB%A3%E5%8F%B2%E7%9A%84%E5%89%8D%E8%BD%A6%E4%B9%8B%E9%89%B4"。\[1\] 另外,JavaScript还提供了encodeURIComponent()函数,用于将URI作为请求参数传递时编码所有的字符。encodeURIComponent()函数会编码所有字符,包括特殊字符和保留字符。\[1\]例如,如果你想编码一个URL参数,比如将参数值文字符编码,你可以使用encodeURIComponent()函数。例如,如果原始URL是"http://localhost:8080/pro?a=1&b=张三&c=aaa",那么使用encodeURIComponent()函数后,编码后的URL将变为"http://localhost:8080/pro?a=1&b=%E5%BC%A0%E4%B8%89&c=aaa"。\[2\] 需要注意的是,encodeURI()和encodeURIComponent()函数都有对应的解码函数,分别是decodeURI()和decodeURIComponent()。这些函数可以用来解码已经编码的URI字符串。\[1\] #### 引用[.reference_title] - *1* [JavaScript encodeURI() 函数详解](https://blog.csdn.net/zyl3133873776/article/details/52022462)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [js使用encodeURI和decodeURI转码和解码](https://blog.csdn.net/xingchen678/article/details/107784720)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值