加入收藏 | 设为首页 | 会员中心 | 我要投稿 湖南网 (https://www.hunanwang.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 创业 > 正文

javaScript中URL编码转换,escape() encodeURI() encodeURIC

发布时间:2018-08-20 07:31:25 所属栏目:创业 来源:站长网
导读:猛火建站学院文档 在行使url举办参数转达时,常常会转达一些中文名的参数或URL地点,在靠山处理赏罚时会产生转换错误。在有些转达页面行使GB2312,而在吸取页面行使UTF8,这样吸取到的参数就也许会与原本产生纷歧致。行使处事器端的urlEncode函数编码的URL,与
  猛火建站学院文档 在行使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 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.

(编辑:湖南网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读