`

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

阅读更多
javaScript中URL编码转换,escape() encodeURI() encodeURIComponent


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

 在使用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.
分享到:
评论

相关推荐

    【JavaScript源代码】基于网址URL中特殊字符转义编码.docx

    基于网址URL中特殊字符...Javascript中的escape,encodeURI和encodeURIComponent的区别 安全字符不同 兼容性不同 对Unicode字符的编码方式不同 适用场合不同 表单提交文档字符集会影响encodeURI吗?其他和Url编码相关

    Javascript中escape(),_encodeURI()和encodeURIComponent()之精析与比较.doc

    Javascript中escape(),_encodeURI()和encodeURIComponent()之精析与比较.doc

    escape、encodeURI 和 encodeURIComponent 的区别

    escape(), encodeURI()和encodeURIComponent()是在Javascript中用于编码字符串的三个常用的方法,而他们之间的异同却困扰了很多的Javascript初学者,今天我就在这里对这三个方法详细地分析与比较一下。

    js中编码函数:escape,encodeURI与encodeURIComponent详解

    而他们之间的异同却困扰了很多的Javascript初学者,这篇文章详细的给大家介绍了js中编码函数:escape,encodeURI与encodeURIComponent的相关资料,需要的朋友可以参考下。

    javascript 对url编码 解码

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

    javascript 字符 Escape,encodeURI,encodeURIComponent

    不会被此方法编码的字符: @ * / + encodeURI() 方法: 把URI字符串采用UTF-8编码格式转化成escape格式的字符串。 不会被此方法编码的字符:! @ # $& * ( ) = : / ; ? + ‘ encodeURIComponent() 方法: 把 URI字符...

    JavaScript encodeURI 和encodeURIComponent

    encodeURI和encodeURIComponet函数都是javascript中用来对URI进行编码,将相关参数转换成UTF-8编码格式的数据。URI在进行定位跳转时,参数里面的中文、日文等非ASCII编码都会进行编码转换

    javascript url几种编码方式详解

    2. encodeURI()是javascript中真正用来对URL编码的函数。编码整个URL地址,但对特殊含义的符号”;/?:@&=+$,#”,也不进行编码。对应的解码函数是decodeURI()。 3. encodeURIComponent()能编码”;/?:@&=+$,#”这些...

    js中字符串编码函数escape()、encodeURI()、encodeURIComponent()区别详解

    JavaScript中有三个可以对字符串编码的函数,分别是: escape,encodeURI,encodeURIComponent,相应3个解码函数: unescape,decodeURI,decodeURIComponent 。 下面简单介绍一下它们的区别 1 escape()函数 定义和...

    JavaScript中URL编码函数代码

    JavaScript中存在几种对URL字符串进行编码的方法:escape(),encodeURI(),以及encodeURIComponent()。这几种编码所起的作用各不相同。 escape() 方法: 采用ISO Latin字符集对指定的字符串进行编码。所有的空格符...

    深入分析escape()、encodeURI()、encodeURIComponent()的区别及示例

    JavaScript中有三个可以对字符串编码的函数,分别是: escape,encodeURI,encodeURIComponent,相应3个解码函数:unescape, decodeURI, decodeURIComponent 。 下面简单介绍一下它们的区别: 1 escape()函数 定义和...

    JavaScript中的编码和解码函数

    js对文字进行编码涉及3个函数:escape,encodeURI,encodeURIComponent,相应3个解码函数:unescape,decodeURI,decodeURIComponent 1、 传递参数时需要使用encodeURIComponent,这样组合的url才不会被#等特殊字符截断...

    字符串编码的机器

    JavaScript中有三个可以对字符串编码的函数,分别是: escape,encodeURI,encodeURIComponent,相应3个解码函数:unescape,decodeURI,decodeURIComponent 。 javascript的简单编码机器和解码机器二合一

    Javascript中的几种URL编码方法比较

    javascript中存在几种对URL字符串进行编码的方法:escape(),encodeURI(),以及encodeURIComponent()。这几种编码所起的作用各不相同。 escape() 方法:  采用ISO Latin字符集对指定的字符串进行编码。所有的空格符...

    js对字符串进行编码的方法总结(推荐)

    在用javascript对URL字符串进行编码中,虽然escape()、encodeURI()、encodeURIComponent()三种方法都能对一些影响URL完整性的特殊字符进行过滤。 但后两者是将字符串转换为UTF-8的方式来传输,解决了页面编码不一至...

    JavaScript中各种编码解码函数的区别和注意事项

    JavaScript 中encodeURI,encodeURIComponent与escape的区别和注

Global site tag (gtag.js) - Google Analytics