URL编码转换,escape() encodeURI() encodeURIComponent()

2016-02-19 15:41 4 1 收藏

岁数大了,QQ也不闪了,微信也不响了,电话也不来了,但是图老师依旧坚持为大家推荐最精彩的内容,下面为大家精心准备的URL编码转换,escape() encodeURI() encodeURIComponent(),希望大家看完后能赶快学习起来。

【 tulaoshi.com - Web开发 】

escape() 方法:

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/webkaifa/)

采用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版本就有。

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/webkaifa/)

英文注释: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.

来源:https://www.tulaoshi.com/n/20160219/1610192.html

延伸阅读
在项目开发过程中,经常会使用到c++ 的url编码和解码,本文将以此问题详细介绍使用技巧,需要的朋友可以参考下 代码如下: #pragma once #include iostream #include string #include windows.h using namespace std; class strCoding { public: strCoding(void); ~strCoding(void); void UTF_8ToGB2312(string &pOut, char *pText,...
标签: Web开发
在xml应用中,经常将一些URL信息作为xml数据存储,其中URL参数有可能包含有中文字符。当使用dom对xml数据进行解析时,可以对中文字符进行编码。 但如果只使用xslt来显示xml数据时(data.xml+data.xsl),发现此时的URL会出现编码错误.即使指定编码类型(encoding="gb2312"),依然会出现同样的问题. 测试发现:是IE的缓存机制问题,IE仍...
  对 String 对象编码以便它们能在所有计算机上可读, escape( charString ) 必选项 charstring 参数是要编码的任意 String 对象或文字。 说明 escape 方法返回一个包含了 charstring 内容的字符串值( Unicode 格式)。所有空格、标点、重音符号以及其他 非 ASCII 字符都用 % xx 编码代替,其中 ...
  将文本字符串编码为一个有效的统一资源标识符 (URI)。 encodeURI( URIString ) 必选的 URIString 参数代表一个已编码的 URI。 说明 encodeURI 方法返回一个编码的 URI。假如您将编码结果传递给 decodeURI ,那么将返回初始的字符串。 encodeURI 方法不会对下列字符进行编码:":"、"/"、";" 和 "?"。请使...
标签: Web开发
UTF-8是UTF-8编码是一种目前广泛应用于网页的编码,它其实是一种Unicode编码,即致力于把全球所有语言纳入一个统一的编码。前UTF-8已经把几种重要的亚洲语言纳入,包括简繁中文和日韩文字。所以在制作某些网站时,需要使用UTF-8,那么怎么把gb2312编码转换到utf-8编码呢?在dreamweaver里只需要一步即可实现。 找到菜单的修改——页面属性——...

经验教程

407

收藏

66

精华推荐

Email+URL的判断和自动转换函数

Email+URL的判断和自动转换函数

摩的摩的0

encodeURIComponent 方法

encodeURIComponent 方法

近看城墙齿齿锯

Javascript &#unicode;和uxxxx编码的转换

Javascript &#unicode;和uxxxx编码的转换

花样花时的年纪

微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部