`
aaron_ch
  • 浏览: 173586 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

Javascript escape(), encodeURI(), encodeURIComponent()

阅读更多

escape():

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."

encodeURI():

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.

encodeURIComponet():

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

When to use which?

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 and the fact that this function fails to handle non-ASCII characters correctly, you should avoid use of escape() whenever possible. The best alternative is usually encodeURIComponent().

escape() will not encode: @*/+

Use of the encodeURI() method is a bit more specialized than escape() in that it encodes for URIs  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.

encodeURI() will not encode: ~!@#$&*()=:/,;?+'

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() will not encode: ~!*()'

分享到:
评论

相关推荐

    escape、encodeURI 和 encodeURIComponent 的区别

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

    JavaScript encodeURI 和encodeURIComponent

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

    javascript 字符 Escape,encodeURI,encodeURIComponent

    下面是对字符串编码转换功能函数大家,大家可以根据实际需要选择使用。

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

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

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

    escape(), encodeURI()和encodeURIComponent()是在Javascript中用于编码字符串的三个常用的方法,而他们之间的异同却困扰了很多的Javascript初学者,这篇文章详细的给大家介绍了js中编码函数:escape,encodeURI与...

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

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

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

    JavaScript中有三个可以对字符串编码的函数,分别是: escape,encodeURI,encodeURIComponent,相应3个解码函数:unescape,decodeURI,decodeURIComponent 。接下来通过本文给大家介绍三者之家的区别,感兴趣的朋友...

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

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

    javascript 对url编码 解码

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

    字符串编码的机器

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

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

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

    JavaScript中的编码和解码函数

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

    url参数中传递特殊字符处理方法

    Java 中提供了三种编码方法:escape、encodeURI 和 encodeURIComponent。它们都可以用于对字符串进行编码,但是它们之间有一些区别: * escape() 方法:采用 ISO Latin 字符集对指定的字符串进行编码。所有的空格符...

    javascript url几种编码方式详解

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

    JavaScript进阶面试题_30题.pdf_前端面试题

    9. JavaScript 中的全局函数和全局变量:JavaScript 中有许多全局函数和全局变量,例如 Infinity、NaN、undefined、decodeURI()、decodeURIComponent()、encodeURI()、encodeURIComponent()、escape()、eval()、is...

    javascript 三种编解码方式

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

    JavaScript权威指南

    JavaScript权威指南 犀牛书 Chapter 1. Introduction to JavaScript Section 1.1. JavaScript Myths Section 1.2. Versions of JavaScript Section 1.3. Client-Side JavaScript Section 1.4. JavaScript ...

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

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

Global site tag (gtag.js) - Google Analytics