`
guanchenglong0220
  • 浏览: 18282 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

JS URL编码转换函数

阅读更多
JS URL编码转换函数

URL编码转换,escape() encodeURI() encodeURIComponent()
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.



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
分享到:
评论

相关推荐

    JS实现URL编码转换中文

    ### JS实现URL编码转换中文 在Web开发过程中,经常需要处理URL中的特殊字符,特别是中文字符。由于URL传输限制及浏览器解析原因,中文等非ASCII字符需要被编码为特定格式才能在网络上传输。本文将详细介绍如何使用...

    url编码转换器

    在这个“url编码转换器”中,我们可以看到它主要服务于将这些特殊字符转换为它们在网络传输中安全的表示形式。 在URL中,非字母数字的字符通常会被转换为"%xy"的形式,其中xy是该字符在ASCII码表中的16进制表示。...

    url编码表,转换编码

    在编程语言中,如JavaScript、Python、Java等,都有内置函数可以方便地对字符串进行URL编码和解码。例如,JavaScript的`encodeURIComponent()`和`decodeURIComponent()`函数,Python的`urllib.parse.quote()`和`...

    URL编码 URL编码

    在实际编程中,大多数编程语言都提供了内置函数来自动进行URL编码和解码,如JavaScript的`encodeURIComponent()`和`decodeURIComponent()`,Python的`urllib.parse.quote()`和`urllib.parse.unquote()`等。...

    基于PHP的URL编码转换工具源码.zip

    【标题】"基于PHP的URL编码转换工具源码"指的是一个使用PHP编程语言编写的程序,该程序的主要功能是进行URL编码与解码。在Web开发中,URL编码是将特殊字符转换为ASCII码的过程,以确保它们能在URL中安全传输。这个...

    unicode格式的字符串进行URL编码

    js函数把base64编码还原成unicode的数据 然后直接调用URL编码函数对数据进行编码返回。就得到易语言支持的url编码unicode字节集的结果。有可能的话我建议精易模块也收录一下或者做一下这方便的URL编码。@龙卷风暴。

    易语言JS实现编码转换源码

    JavaScript的内建函数如`decodeURI()`, `decodeURIComponent()`, `encodeURIComponent()` 和 `encodeURI()` 提供了基本的编码和解码功能,但它们主要针对URL编码。对于更复杂的编码转换,如GBK到UTF-8,或者反之,...

    纯Javascript脚本实现GBK URL编解码

    2. **URL编码**:JavaScript的`encodeURIComponent`函数可以对字符串进行URL编码,但默认它只处理UTF-8编码。为了GBK编码的字符串能正确编码,需要先将GBK编码后的字节序列转换为 `%xx` 形式,这可以通过遍历字节...

    一个简单url编码解码

    每种语言都有其特定的库或内置函数来处理URL编码和解码,学习者可以通过这个项目了解如何在实际编程中应用这些功能。 7. **实践应用**:理解URL编码解码不仅有助于开发Web应用程序,还对网络爬虫、数据分析和API...

    利用JS把URL地址栏%20、%55之类编码转成中文字符

    本文将深入探讨如何使用JavaScript(简称JS)来解码这些URL编码,使其转换回原本的中文或其他非英文字符。 ### URL编码与解码原理 URL编码的主要目的是确保URL中的所有字符都能在网络上传输,避免因特殊字符导致的...

    urlcode解码-HTTP:URL编码解码

    另外,虽然在大多数情况下,URL编码遵循ASCII标准,但在Unicode环境中,URL编码也可以扩展到包含非ASCII字符,这通常被称为“ Punycode”转换。 在实际开发中,了解并正确使用URL编码和解码不仅能保证数据的完整性...

    ASP URL反编码函数代码

    URL编码是一种编码方式,用于在网络上传输数据时,对URL中不能正常传输的字符进行编码转换。它是将非ASCII字符、特殊字符等转换为URL可以传输的格式,即在百分号(%)后面跟随两个十六进制数字。当这些数据需要被接收...

    URL 编码文档

    URL编码是一种在网络应用中将特殊字符转换为安全的ASCII字符序列的过程,以确保URL的正确传输和解析。URL,全称Uniform Resource Locator,是统一资源定位符,是互联网上资源的唯一地址。实际上,URL是URI(统一资源...

    URL编码解码器

    在JavaScript中,`encodeURIComponent()`和`decodeURIComponent()`函数分别用于编码和解码。 4. **Unicode支持**:现代的URL编码不仅支持ASCII字符,还支持Unicode字符集。在URL中,一个Unicode字符可能需要编码为...

    URL解码-编码器URL解码-编码器

    在实际操作中,开发者通常会使用编程语言提供的内置函数进行URL编码和解码,例如JavaScript的`encodeURIComponent()`和`decodeURIComponent()`,Python的`urllib.parse.quote()`和`urllib.parse.unquote()`,Java的`...

    url编码

    1. **编程语言内置函数**:在大多数编程语言中,如Java、Python、JavaScript,都有内置的函数来处理URL编码和解码,例如JavaScript的 `encodeURIComponent()` 和 `decodeURIComponent()` 函数。 2. **在线工具**:...

    解析URL和文件的编码方式

    编码过程通常由编程语言提供的内置函数自动完成,如JavaScript的`encodeURIComponent()`函数。 文件编码则涉及到文件内容的存储方式。常见的文本文件编码有ASCII、UTF-8、GBK等。ASCII是最基础的编码,只包含128个...

    url的三个js编码函数escape(),encodeURI(),encodeURIComponent()简介

    `escape()`函数是JavaScript中较早出现的一种URL编码方式。它会将字符串转换成适用于URL的部分Unicode编码形式。例如: ```javascript console.log(escape("春天")); // 输出:%u6625%u8282 ``` 可以看到,`escape...

    UrlEncode编码转换.zip

    这个压缩包文件"UrlEncode编码转换.zip"很可能包含了关于如何进行URL编码转换的相关教程或工具。在这个讨论中,我们将深入探讨URL编码的原理、用途以及如何进行编码和解码。 URL编码是因特网标准RFC 3986定义的一...

    JavaScript中URL编码函数代码

    在JavaScript中,URL编码是处理URL字符串的重要环节,确保数据在网络传输过程中不会因特殊字符引起解析错误。有三种主要的函数用于对URL进行编码:`escape()`,`encodeURI()`和`encodeURIComponent()`。它们各自有...

Global site tag (gtag.js) - Google Analytics