`
aaron_ch
  • 浏览: 178095 次
  • 性别: 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 区别详解 在前端开发中,经常需要用到字符串编码与解码的方法来确保数据在网络传输中的正确性与安全性。本文将详细介绍 `escape()`、`encodeURI()` 和 `...

    谈谈encodeURI和encodeURIComponent以及escape的区别与应用

    在JavaScript中,`encodeURI()`、`encodeURIComponent()`和`escape()`是三个用于字符串编码的函数,它们的主要目的是确保特殊字符在传输过程中不会引起错误。理解它们之间的差异对于编写正确处理URL和数据的...

    javascript 字符 Escape,encodeURI,encodeURIComponent

    本文将详细介绍`escape()`、`encodeURI()`和`encodeURIComponent()`这三个函数,并解释它们的区别和使用场景。 首先,`escape()`方法是JavaScript最早提供的一个编码函数,它基于ISO Latin字符集对字符串进行编码。...

    escape、encodeURI 和 encodeURIComponent 的区别

    在JavaScript中,`escape()`, `encodeURI()`, 和 `encodeURIComponent()` 是三个常见的字符串编码函数,它们用于处理和转换字符串中的特殊字符。虽然它们都与字符串编码有关,但各自的作用和适用场景有所不同。 ...

    JS中三种编码方式(escape,encodeURI,encodeURIComponent)

    总结来说,理解并正确使用`escape`、`encodeURI`和`encodeURIComponent`是JavaScript开发中的重要技能,它们有助于确保数据在网络中的安全传输和正确解析。在处理URL、查询参数或其他需要编码的数据时,要根据具体...

    JavaScript encodeURI 和encodeURIComponent

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

    简单明了区分escape、encodeURI和encodeURIComponent

    本文主要关注三个与URL编码相关的函数:`escape`、`encodeURI`和`encodeURIComponent`。这些函数各有其特定的用途,理解它们的区别至关重要。 首先,我们来看`escape`函数。`escape`并不专门用于URL编码,它实际上...

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

    在JavaScript中,`escape()`, `encodeURI()`, 和 `encodeURIComponent()` 是三个常见的字符串编码函数,它们各自有独特的用途和特点。理解它们的区别对于编写健壮的JavaScript代码至关重要。 首先,`escape()` 函数...

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

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

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

    ### URL的三个JS编码函数:`escape()`, `encodeURI()`, `encodeURIComponent()` 简介 在Web开发中,经常遇到的一个问题是URL传递中文字符时出现乱码的情况。为了解决这一问题,JavaScript提供了几种不同的编码方法...

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

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

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

    JavaScript中的字符串编码函数主要包括`escape()`、`encodeURI()`和`encodeURIComponent()`,它们的作用是对字符串进行编码处理,以便在网络中安全地传输数据。这三种函数都有对应的解码函数:`unescape()`、`...

    ZzJavaScript encode and escape functions

    JavaScript中的编码和转义函数主要有encodeURI、encodeURIComponent和escape这三个函数,它们用于处理URL和URI中的特殊字符。下面将详细介绍这三个函数的功能和使用场景。 首先,encodeURI函数的主要用途是编码整个...

Global site tag (gtag.js) - Google Analytics