2
2
2009
11

[javascript]javascript的URL编码和解码

在 使用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.

 
1.编码处理函数
1) encodeURI 返回一个对URI字符串编码后的结果。URL是最常见的一种URI;
2) decodeURI 将一个已编码的URI字符串解码成最原始的字符串返回;
3) 举例: < Script language = " javascript " > 输出结果如下: encodeStr: http://www.amigoxie.com/index.jsp?name=%E9%98%BF%E8%9C%9C%E6%9E%9C decodeStr: http://www.amigoxie.com/index.jsp?name=xind
 
2. 数值处理函数
1) parseInt 将一个字符串指定的进制转换为一个整数,语法格式为: parseInt(numString, [radix]) 第一个参数是要进行转换的字符串,是介于2到36之间的数值,用于指定进行字符串转换时所用的进制。 举例如下: 输出结果如下: 默认情况下的结果:32:32;032:26;0x32:50 转为2进制的结果:32:NaN;032:0;0x32:0 转为8进制的结果:32:26;032:26;0x32:0 转为16进制的结果:32:50;032:50;0x32:50 11001010转换后的结果: 2进制:202;16进制:285216784 8进制:2359816;10进制:11001010 43abc转换后:43;abc43转换后:NaN;abc转换后:NaN
2) parseFloat方法 该方法将一个字符串转换成对应的小数。 eg. 输出结果如下: 4.11 5.1 3) isNaN方法 该方法用于检测前两个方法返回值是否为非数值型,如果是,返回true,否则,反回false
 
转至:http://hi.baidu.com/blueyund/blog/item/dc060bd1bb8e5cd4562c8436.html
Category: JavaScript | Tags: javascript url编码 | Read Count: 1997
Avatar_small
Ipsa quidem iure ips 说:
2018年6月18日 23:07

The significance of the programmer has been built for the utility for all individuals. The mild approach of the programmer and academic writing assistance has been implied for the decoration for all individuals for the softening of the images for the candidates.

Avatar_small
LeoField 说:
2018年12月03日 21:07

I am recently joined in a company and doing a project on Java. I don't have much knowledge in programming. This site is really helpful for me to learn the Java pragramming.

moonlight sonata

Avatar_small
northeastfactorydire 说:
2019年9月27日 20:23

You can <a href="https://www.northeastfactorydirect.com/">get discount furniture outlet</a> in Ohio we have the best disocunt rate furniture store in Ohio.

Avatar_small
thebasementguyscleve 说:
2019年9月27日 21:59

You can get the best bowing basement walls repair service in Ohio with our experts in your budget just let our experts know about your budget they can provide you can best service in Ohio.

Avatar_small
cleaning services 说:
2019年10月13日 02:31

Visualize having longer to Rest. Imagine forthcoming home to help sparkling fresh. Our clean-up system has become developed in addition to perfected intended for over 9 A long time. We unique latest clean-up tools to decontaminate your villas, apartments rentals every spot in just hygienic approach. From porches to help garage in addition to bedroom to help living spot and kitchen's, we talk with full devotion for making your villas glimmer in brightness and twinkle in nights because of their clean in addition to tidy overall look.

Avatar_small
dui lawyer montreal 说:
2020年4月16日 00:09

Whatever relationship approximately two choices, either customers or firms, cannot come to be established except prior to some range rules. These rules could very well be unenforceable norms or possibly customs from the group or possibly society, or a lot of explicit laws getting a binding and additionally enforceable recognized.

Avatar_small
wendy wood law 说:
2020年4月16日 00:09

Might be identified characteristics that particular one should have to achieve a position in regularions. You might assess your body before literally enrolling right into any group. To enjoy a clear option, you may well visit city courts, give priority to trials, chat with lawyers and take notice of the functioning for the legal model.

Avatar_small
star lite shopping m 说:
2020年4月16日 00:10

When we finally talk approximately fashion, a wonderfully hot on the lookout lady takes place reverberating in this particular mind. The real reason for this is normally fashion is mostly a replica about beauty, foundation and trend, that to get seen for women. But it doesn't stop here mean which usually male really are behind the style scene.

Avatar_small
shopping batalha 说:
2020年4月16日 00:10

All of these aquarium outlet stores have just about every single stock items that you may require this kind of means you do not have to go elsewhere to ensure you get your stuff. You’re able to avail the whole thing here right from heaters, filter systems, lights, food indeed fish! They also offer stands, indoor plants and tanks.

Avatar_small
elimperio travel 说:
2020年4月16日 00:10

Decreasing benefit extremely effective flexibility on the self-planned travel around holidays. This particular form about travel, you may well customize all the self-planned travel as a result of the last second details.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter

Host by is-Programmer.com | Power by Chito 1.3.3 beta | Theme: Aeros 2.0 by TheBuckmaker.com