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

 更新时间:2006年12月27日 00:00:00   作者:  
escape() 方法:
采用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版本就有。

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




相关文章

  • javascript的几种写法总结

    javascript的几种写法总结

    下面小编就为大家带来一篇javascript的几种写法总结。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-09-09
  • 关于JavaScript对象类型之Array及Object

    关于JavaScript对象类型之Array及Object

    这篇文章主要介绍了关于JavaScript对象类型之Array及Object,Array 类型是 ECMAScript 中最常用的类型了。而且,ECMAScript 中的数组与其他多数语言中的数组有着相当大的区别,需要的朋友可以参考下
    2023-05-05
  • Typescript中的as、问号与感叹号详解

    Typescript中的as、问号与感叹号详解

    这篇文章主要介绍了Typescript中的as、问号与感叹号详解,本文分别讲述了这几个关键字的含义作用以及实例,通过文字和代码的描述,详细的表达.以下就是详细内容,需要的朋友可以参考下
    2021-07-07
  • JavaScript中函数的常用写法及调用方法

    JavaScript中函数的常用写法及调用方法

    这篇文章介绍了JavaScript中函数的常用写法及调用方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-06-06
  • Javascript入门学习第五篇 js函数

    Javascript入门学习第五篇 js函数

    上篇文章讲了js中对象和数组的一些方法。 这章我们先说说函数,然后来点实战。
    2008-07-07
  • 屏蔽alt+f4代码,一个变通的方法

    屏蔽alt+f4代码,一个变通的方法

    屏蔽alt+f4代码,一个变通的方法...
    2006-10-10
  • JavaScript 表单处理实现代码

    JavaScript 表单处理实现代码

    这篇文章主要介绍了JavaScript 表单处理实现代码,需要的朋友可以参考下
    2015-04-04
  • js四舍五入数学函数round使用实例

    js四舍五入数学函数round使用实例

    这篇文章主要介绍了js四舍五入数学函数round使用实例,需要的朋友可以参考下
    2014-05-05
  • js函数调用常用方法详解

    js函数调用常用方法详解

    js的函数调用会免费奉送两个而外的参数就是 this 和 arguments,我们所知道的应该有4中调用方式吧,接下来将为您详细介绍,感兴趣的朋友可以参考下
    2012-12-12
  • php与js的区别是什么

    php与js的区别是什么

    php与js的区别包括:类型转换的不同、实形参的不同、数据类型的不同等等,更多参考下文,希望对大家有所帮助
    2013-08-08

最新评论