前端汉字encode_JavaScriptURL汉字编码转换
在使⽤url进⾏参数传递时,经常会传递⼀些中⽂名的参数或URL地址,在后台处理时会发⽣转换错误。在有些传递页⾯使⽤GB2312,⽽在接收页⾯使⽤UTF8,这样接收到的参数就可能会与原来发⽣不⼀致。使⽤服务器端的urlEncode函数编码的URL,与使⽤客户端javascript的encodeURI函数编码的URL,结果就不⼀样。
JavaScript对⽂字进⾏编码涉及3个函数:escape,encodeURI,encodeURIComponent,相应3个解码函数:
unescape,decodeURI,decodeURIComponent。
效果演⽰
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.
就是JavaScript使⽤数据时可以使⽤escape()函数。
escape对0-255以外的unicode值进⾏编码时输出%u****格式,其它情况下escape,encodeURI,encodeURIComponent编码结果相同。
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.在线url网址编码解码
进⾏url跳转时可以整体使⽤encodeURI(),⽐如:
Location.href=encodeURI("wamagic/");
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.
传递参数时需要使⽤encodeURIComponent,这样组合的url才不会被#等特殊字符截断。例如:
document.write('');
因此,对于中⽂字符串来说,如果不希望把字符串编码格式转化成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 most cases when encoding a single component of a URI. This method will encode certain chars that would normally be recognized as special chars for URIs so that many components may be included. Note that this method does not encode the ' character, as it is a valid character within URIs.
最多使⽤的应为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。
escape(str) ⽅法,它⽤于转义不能⽤明⽂正确发送的任何字符。⽐如,电话号码中的空格将被转换成字符 %20,从⽽能够在 URL 中传递这些字符
如果需要发送安全信息或 XML,可能要考虑使⽤ send() 发送内容(本系列的后续⽂章中将讨论安全数据和 XML 消息)。如果不需要通过send() 传递数据,则只要传递 null 作为该⽅法的参数即可。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。