URL编码转换,escape() encodeURI() encodeURIComponent()_基础知识

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.

时间: 2024-07-30 07:15:30

URL编码转换,escape() encodeURI() encodeURIComponent()_基础知识的相关文章

通过正则表达式获取url中参数的简单实现_基础知识

url:      http://xxxx.com?name=魅力&id=123 js中: var name = getUrlParam("name"); /*通过正则获取url中的参数*/ function getUrlParam(name){ var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)"); var r = window.location.search.

解析URI与URL之间的区别与联系_基础知识

今天在看STRUTS配置的时候,发现一个问题,就是在看配置文件的时候,有时出现URL有时又是URI, 让我心生不解,到网上查了一圈,解释都含糊不清, 索性自己总结一下,仅代表个人理解,由于本人知识实在有限,可能会有错误,不过我认为是应该这样的. 总结如下: 1.简写:URI (uniform resource identifier)统一资源标志符:URL(uniform resource location )统一资源定位符(或统一资源定位器):URN(uniform resource name

NodeJS url验证(url-valid)的使用方法_基础知识

Javascript做url检验,通常是使用正则表达式来判定,其格式是否正确,例如: 复制代码 代码如下: /^https?:\/\//.test(url); 当然还有更好的检测方法比如基于RFC 3986, RFC 3966, RFC 4694, RFC 4759, RFC 4904等标准的进行验证的valid-url库.不过个根据格式进行验证当然不能确定该url是否存在啦,所以就有了url-valid,我们基于HTTP请求进行验证. 接口设计实际上我们只需要一个函数传入一个url地址,并回调

js中escape,encodeURI,encodeURIComponent 区别(转)

js对文字进行编码涉及3个函数:escape,encodeURI,encodeURIComponent,相应3个解码函数:unescape,decodeURI,decodeURIComponent 1. 传递参数时需要使用encodeURIComponent,这样组合的url才不会被#等特殊字符截断. 例如:<script language="javascript">document.write('<a href="http://passport.baidu

escape,encodeURI,encodeURIComponent函数比较

比较|函数|escape|encodeuri|encodeuricomponent js对文字进行编码涉及3个函数:escape,encodeURI,encodeURIComponent,相应3个解码函数:unescape,decodeURI,decodeURIComponent 1.  传递参数时需要使用encodeURIComponent,这样组合的url才不会被#等特殊字符截断.                            例如:<script language="java

js编码、解码函数介绍及其使用示例_基础知识

js对文字进行编码涉及3个函数:escape,encodeURI,encodeURIComponent,相应3个解码函数:unescape,decodeURI,decodeURIComponent 1.传递参数时需要使用encodeURIComponent,这样组合的url才不会被#等特殊字符截断. 例如: <script language="javascript">document.write('<a href="http://passport.baidu

js之WEB开发调试利器:Firebug 下载_基础知识

在米随随的Blog看到有关Firebug的介绍,遂下载试用了一下,确实是比较好的工具. 一.效果开发调试利器:Firebug 下载_基础知识-电工基础知识视频下载"> 二.主要功能 Inspect and edit HTML Tweak CSS to perfection Visualize CSS metrics Monitor network activity Debug and profile JavaScript Quickly find errors Explore the DO

js escape() encodeURI() encodeURIComponent编码问题

escape() 方法: 采用iso latin字符集对指定的字符串进行编码.所有的空格符.标点符号.特殊字符以及其他非ascii字符都将被转化成%xx格式的字符编码(xx等于该字符在字符集表里面的编码的16进制数字).比如,空格符对应的编码是%20.unescape方法与此相反.不会被此方法编码的字符: @ * / + . 定义和用法 encodeuricomponent() 函数可把字符串作为 uri 组件进行编码. 语法 encodeuricomponent(uristring)参数 描述

浅谈JavaScript中的字符编码转换问题_基础知识

要获得字符的Unicode编码,可以使用string.charCodeAt(index)方法,其定义为:    strObj.charCodeAt(index)       index为指定字符在strObj对象中的位置(基于0的索引),返回值为0与65535之间的16位整数.例如: var strObj = "ABCDEFG"; var code = strObj.charCodeAt(2); // Unicode value of character 'C' is 67