已阅读5页,还剩3页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
escape,encodeURI,encodeURIComponent在使用url进行参数传递时,经常会传递一些中文名(或含有特殊字符)的参数或URL地址,在后台处理时会发生转换错误。在有些传递页面使用GB2312,而在接收页面使用UTF8,这样接收到的参数就可能会与原来发生不一致。使用服务器端的 urlEncode函数编码的URL,与使用客户端javascript的encodeURI函数编码的URL,结果就不一样。javascript对文字进行编码涉及3个函数:escape,encodeURI,encodeURIComponent,相应3个解码函数:unescape,decodeURI,decodeURIComponentjavaScript中的编码方法:escape() 方法:采用ISO Latin字符集对指定的字符串进行编码。所有的空格符、标点符号、特殊字符以及其他非ASCII字符都将被转化成%xx格式的字符编码(xx等于该字符在字符集表里面的编码的16进制数字)。比如,空格符对应的编码是%20。unescape方法与此相反。不会被此方法编码的字符: * / +encodeURI()方法:把URI字符串采用UTF-8编码格式转化成escape格式的字符串。不会被此方法编码的字符:! # $& * ( ) = : / ; ? + encodeURIComponent()方法:把URI字符串采用UTF-8编码格式转化成escape格式的字符串。与encodeURI()相比,这个方法将对更多的字符进行编码,比如 / 等字符。所以如果字符串里面包含了URI的几个部分的话,不能用这个方法来进行编码,否则 / 字符被编码之后URL将显示错误。不会被此方法编码的字符:! * ( )因此,对于中文字符串来说,如果不希望把字符串编码格式转化成UTF-8格式的(比如原页面和目标页面的charset是一致的时候),只需要使用escape。如果你的页面是GB2312或者其他的编码,而接受参数的页面是 UTF-8编码的,就要采用encodeURI或者encodeURIComponent。另外,encodeURI/encodeURIComponent是在javascript1.5之后引进的,escape则在javascript1.0版本就有。1、 传递参数时需要使用encodeURIComponent,这样组合的url才不会被#等特殊字符截断。例如:document.write(a href=/?logout&aid=7&a.gt;);2、 进行url跳转时可以整体使用encodeURI。例如:Location.href=encodeURI(/do/s?word=百度&ct=21);3、 js使用数据时可以使用escape。例如:搜藏中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-ZencodeURI不编码字符有82个:!,#,$,&,(,),*,+,,,-,.,/,:,;,=,?,_,0-9,a-z,A-ZencodeURIComponent不编码字符有71个:!, ,(,),*,-,.,_,0-9,a-z,A-Z 解决URL中文乱码问题(二) 当在页面上遇到链接上传输的参数为中文,而且在后台接收时为乱码时,可以在页面上对其进行编码encodeURI(中文);将其用UTF-8格式进行编码,然后在后台用 String filename = request.getParameter(filename); filename = new String(filename.getBytes(ISO8859_1), UTF-8); 得到的filename参数即为UTF-8格式的中文 javaScript中URL编码转换,escape() encodeURI() encodeURIComponent javaScript中URL编码转换,escape() encodeURI() encodeURIComponent 2007年05月12日 星期六 下午 04:48 在使用url进行参数传递时,经常会传递一些中文名的参数或URL地址,在后台处理时会发生转换错误。在有些传递页面使用GB2312,而在接收页面使用UTF8,这样接收到的参数就可能会与原来发生不一致。使用服务器端的urlEncode函数编码的URL,与使用客户端javascript的encodeURI函数编码的URL,结果就不一样。 javaScript中的编码方法: escape() 方法: 采用ISO Latin字符集对指定的字符串进行编码。所有的空格符、标点符号、特殊字符以及其他非ASCII字符都将被转化成%xx格式的字符编码(xx等于该字符在字符集表里面的编码的16进制数字)。比如,空格符对应的编码是%20。unescape方法与此相反。不会被此方法编码的字符: * / + 英文解释:MSDN JScript Reference: The escape method returns a string value (in Unicode format) that contains the contents of the argument. All spaces, punctuation, accented characters, and any other non-ASCII characters are replaced with %xx encoding, where xx is equivalent to the hexadecimal number representing the character. For example, a space is returned as %20. Edge Core Javascript Guide: The escape and unescape functions let you encode and decode strings. The escape function returns the hexadecimal encoding of an argument in the ISO Latin character set. The unescape function returns the ASCII string for the specified hexadecimal encoding value. encodeURI() 方法:把URI字符串采用UTF-8编码格式转化成escape格式的字符串。不会被此方法编码的字符:! # $& * ( ) = : / ; ? + 英文解释:MSDN JScript Reference: The encodeURI method returns an encoded URI. If you pass the result to decodeURI, the original string is returned. The encodeURI method does not encode the following characters: :, /, ;, and ?. Use encodeURIComponent to encode these characters. Edge Core Javascript Guide: Encodes a Uniform Resource Identifier (URI) by replacing each instance of certain characters by one, two, or three escape sequences representing the UTF-8 encoding of the character encodeURIComponent() 方法:把URI字符串采用UTF-8编码格式转化成escape格式的字符串。与encodeURI()相比,这个方法将对更多的字符进行编码,比如 / 等字符。所以如果字符串里面包含了URI的几个部分的话,不能用这个方法来进行编码,否则 / 字符被编码之后URL将显示错误。不会被此方法编码的字符:! * ( ) 英文解释:MSDN JScript Reference: The encodeURIComponent method returns an encoded URI. If you pass the result to decodeURIComponent, the original string is returned. Because the encodeURIComponent method encodes all characters, be careful if the string represents a path such as /folder1/folder2/default.html. The slash characters will be encoded and will not be valid if sent as a request to a web server. Use the encodeURI method if the string contains more than a single URI component. Mozilla Developer Core Javascript Guide: Encodes a Uniform Resource Identifier (URI) component by replacing each instance of certain characters by one, two, or three escape sequences representing the UTF-8 encoding of the character. 因此,对于中文字符串来说,如果不希望把字符串编码格式转化成UTF-8格式的(比如原页面和目标页面的charset是一致的时候),只需要使用escape。如果你的页面是GB2312或者其他的编码,而接受参数的页面是UTF-8编码的,就要采用encodeURI或者encodeURIComponent。 另外,encodeURI/encodeURIComponent是在javascript1.5之后引进的,escape则在javascript1.0版本就有。 英文注释:The escape() method does not encode the + character which is interpreted as a space on the server side as well as generated by forms with spaces in their fields. Due to this shortcoming, you should avoid use of escape() whenever possible. The best alternative is usually encodeURIComponent().Use of the encodeURI() method is a bit more specialized than escape() in that it encodes for URIs REF as opposed to the querystring, which is part of a URL. Use this method when you need to encode a string to be used for any resource that uses URIs and needs certain characters to remain un-encoded. Note that this method does not encode the character, as it is a valid character within URIs.Lastly, the encodeURIComponent() method should be used in
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年股权代持协议书格式
- 康复机器人辅助下的关节活动度训练
- 度普利尤单抗治疗AD的个体化治疗策略
- 应急演练效果评估报告撰写
- 库欣综合征的ACTH依赖型病因诊断策略
- 年轻化策略与Z世代吸引力品牌-1
- 帕金森病患者步态稳定性的机器人干预研究
- 帕金森病基因编辑微创治疗的手术器械创新
- 医疗单位礼仪培训实施策略
- 妇产科护理培训课程
- 部编版(2024)小学语文三年级上册期末综合质量调研卷(含答案)
- 雨课堂在线学堂《项目管理概论》作业单元考核答案
- 诊所注销申请书
- 心脏瓣膜病麻醉管理
- TBT3208-2023铁路散装颗粒货物运输防冻剂
- 航天禁(限)用工艺目录(2021版)-发文稿(公开)
- 汽车吊、随车吊起重吊装施工方案
- 中外政治思想史练习题及答案
- 降低阴式分娩产后出血发生率-PDCA
- GB/T 5211.6-2020颜料和体质颜料通用试验方法第6部分:水悬浮液pH值的测定
- GB/T 36024-2018金属材料薄板和薄带十字形试样双向拉伸试验方法
评论
0/150
提交评论