Comparing escape()
, encodeURI()
, andencodeURIComponent()
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('~!@#$%^&*(){}[]=:/,;?+\'"\\'):
-->
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('~!@#$%^&*(){}[]=:/,;?+\'"\\'):
-->
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('~!@#$%^&*(){}[]=:/,;?+\'"\\'):
-->
All three of them
-->
escape()
:
encodeURI()
:
encodeURIComponent()
:
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 URIsand 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
Credits: yossarian and Jumper on #javascript (EFNet) for editing and consulting; Joseph Tatroult for additional test cases.
Brad Fults
分享到:
相关推荐
论文 Comparing Linear Discriminant Analysis and Support Vector Machine. 2002. 欢迎下载。
很经典的一篇分析和比较蒙哥马利模乘不同的实现方式的文章
Characterizing and Comparing Phylogenies from their Laplacian Spectrum
Comparing Realism and Idealism as Classic Schools of Thought
Attention Flows:Analyzing and Comparing Attention Mechanisms in Language Models
IR Models\\IR Models\\Comparing Boolean and Probabilistic IR Systems
英文文档,多角度比较ORB和AKAZE两种算法的优劣,对特征提取算法的选择有参考价值。
Performance Comparing and Analysis for Slot Allocation Model
nslations as Manipulation and Rewriting- Comparing the Different Translations-论文.zip
NULL 博文链接:https://angkor.iteye.com/blog/1047736
使用 AspectJ 和 Java design pattern 的比较..
hash code comparing objects in java collections.
AIX、redhat enterprise和solaris操作系统的比较
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 ...
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.
There is a detailed segment comparing solid-state drives (SSDs) to conventional hard disk drives (HDDs), explaining all the differences and similarities between them in an easy to understand fashion....
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
Modeling and Simulation Techniques in Structural Engineering (Advances in Civil and Industrial Engineering) By 作者: Pijush Samui ISBN-10 书号: 1522505881 ISBN-13 书号: 9781522505884 Edition 版本: 1 ...