The purpose of this article is to examine the differences between these three methods and decide on the appropriate times to use each.
escape()
method
Defined in: JavaScript 1.0
Descriptions
MSDN JScript Reference [REF]
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."
Mozilla Developer Core Javascript Guide [REF]
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.
Example
The easiest way to understand the behavior is to see it:
escape('~!@#$%^&*(){}[]=:/,;?+\'"\\'):
'%7E%21@%23%24%25%5E%26*%28%29%7B%7D%5B%5D%3D%3A/%2C%3B%3F+%27%22%5C'
encodeURI()
method
Defined in: JavaScript 1.5
Descriptions
MSDN JScript Reference [REF]
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.
Mozilla Developer Core Javascript Guide [REF]
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.
Example
The easiest way to understand the behavior is to see it:
encodeURI('~!@#$%^&*(){}[]=:/,;?+\'"\\'):
'~!@#$%25%5E&*()%7B%7D%5B%5D=:/,;?+\'%22%5C'
encodeURIComponent()
method
Defined in: JavaScript 1.5
Descriptions
MSDN JScript Reference [REF]
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 [REF]
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.
Example
The easiest way to understand the behavior is to see it:
encodeURIComponent('~!@#$%^&*(){}[]=:/,;?+\'"\\'):
'~!%40%23%24%25%5E%26*()%7B%7D%5B%5D%3D%3A%2F%2C%3B%3F%2B\'%22%5C'
Summary: What does this mean?
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 [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.
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: ~!*()'
References
MSDN JScript Reference - escape(), encodeURI(), encodeURIComponent()
Mozilla Developer Core Javascript Guide - escape(), encodeURI(), encodeURIComponent()
ASCII Table - http://www.asciitable.com/
W3C's URIs, URLs, and URNs: Clarifications and Recommendations 1.0 - http://www.w3.org/TR/uri-clarification/
相关推荐
“Analyzing and Comparing Montgomery Multiplication Algorithms”(分析与比较蒙哥马利模乘算法)这篇文章旨在深入探讨并对比不同的蒙哥马利模乘算法实现方法。蒙哥马利模乘算法是一种在计算机科学中广泛应用的...
Comparing Realism and Idealism as Classic Schools of Thought
2002年发表的论文《Comparing Linear Discriminant Analysis and Support Vector Machine》对比了这两种算法在分类任务中的表现和特点。由于文档内容是由OCR扫描技术处理,可能包含文字识别错误或遗漏。不过,我们仍...
在内容部分中,提到的文章“Characterizing and Comparing Phylogenies from their Laplacian Spectrum”发表在系统生物学杂志(Systematic Biology)上,卷号为65,期号为3,页码范围495-507。文章是通过拉普拉斯谱...
ORB(Oriented FAST and Rotated BRIEF)和AKAZE(Accelerated-KAZE)是两种用于此目的的算法,它们分别代表了不同的技术路线。 ORB算法基于FAST关键点检测器和BRIEF描述子,它们都经过了优化以提高速度和旋转不变...
本文《Attention Flows:Analyzing and Comparing Attention Mechanisms in Language Models》是一篇研究论文,主要关注于深度学习领域内语言模型的注意力机制。随着自然语言处理(NLP)技术的发展,基于注意力机制...
文章“Comparing the Struts 1 and Struts 2 Web Application Frameworks”可能详细分析了这两个框架的各个方面,包括但不限于配置方式、开发效率、错误处理、性能测试等。通过阅读文档,开发者可以获取更多实用的...
### 设计模式在Java与AspectJ中的对比分析 #### 背景介绍 设计模式作为软件工程领域的重要组成部分,旨在解决软件开发过程中遇到的常见问题。随着面向对象编程的普及,设计模式得到了广泛的应用和发展。...
在该模型中,查询被表示为一个由关键词组成的集合,这些关键词之间通过布尔运算符(如AND、OR、NOT等)连接起来。布尔模型的关键特征在于它将文档检索视为一个二值决策问题:文档要么满足查询条件(true),要么不...
标题中的“nslations as Manipulation and Rewriting- Comparing the Different Translations”涉及的是翻译研究的一个重要领域,即翻译的操纵与改写,并通过比较不同的翻译版本来探讨这一主题。这篇论文可能深入...
### 操作系统对比分析:Solaris与Red Hat Enterprise Linux及AIX 在当前技术环境中,随着Oracle收购Sun Microsystems带来的变化,对于那些长期依赖Solaris的操作系统管理员而言,这是一个充满挑战的时代。...
CANdb++ 3.0 SP27 最新...Error correction when comparing messages if they are identified by their names. Error correction when exporting databases, if CANdb++ Admin. J1939 is installed under C: \Programs
在`Comparing Java objects with equals and hashcode.pdf`文档中,可能会详细讨论这些方法的实现细节、最佳实践和潜在陷阱,例如: - 如果重写了`equals()`,也应该重写`hashCode()`,以遵循合同约定。 - 当`...
How to retrieve, update, and delete data quickly and logically using LINQ with side-by-side examples comparing LINQ to existing techniques * Ways to localize your web site in multiple languages ...
根据给定的文件信息,以下是对标题和描述中所蕴含的知识点的详细说明: 1. 标题和描述表明这篇文章主要研究的是“Slot Allocation Model”的性能比较与分析。Slot Allocation Model指的是飞机起降时刻(slot)分配...
根据提供的文件信息,这篇文章的标题是《Biomass to methane and ethanol的net energy yield比较研究》。文章的描述指出,它是由黄卫东和夏维东撰写的,文章对生物质发酵生产酒精和厌氧消化生产甲烷两种工艺的转化率...
Customize the MVVM pattern for your projects needs by comparing the various implementation styles In Detail MVVM (Model View View Model) is a Microsoft best practices pattern for working in WPF and...
Chapter 5 Comparing Groups: Tables and Visualizations Chapter 6 Comparing Groups: Statistical Tests Chapter 7 Identifying Drivers of Outcomes: Linear Models Part III Advanced Marketing Applications ...
随书前言如下:Learn Data Structures and Algorithms with Go covers ... This book explains the concepts for comparing algorithm complexity and data structures in terms of code performance and efficiency.