javaScript中URL编码换,escape() encodeURI() encodeURIC

     在使用url进行参数传递时,经常会传递一些中文名的参数或url地址,在后台处理时会发生转换错误。在有些传递页面使用gb2312,而在接收页面使用utf8,这样接收到的参数就可能会与原来发生不一致。使用服务器端的urlencode函数编码的url,与使用客户端javascript的 encodeuri函数编码的url,结果就不一样。
javascript中的编码方法:
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. 

你可能感兴趣的:(java,工作)