`
bulargy
  • 浏览: 64955 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

javascript的URL编码和解码

    博客分类:
  • js
阅读更多
在 使用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://www.cnblogs.com/dyc988/archive/2008/07/22/1248461.html
分享到:
评论

相关推荐

    javascript URL编码和解码使用说明

    使用服务器端的urlEncode函数编码的URL,与使用客户端javascript的 encodeURI函数编码的URL,结果就不一样。 javaScript中的编码方法: escape() 方法: 采用ISO Latin字符集对指定的字符串进行编码。所有的空格符、...

    纯Javascript脚本实现GBK URL编解码

    纯 Javascript 脚本实现 GBK URL 编码和解码

    javascript 对url编码 解码

    js对文字进行编码涉及3个函数:escape,encodeURI,encodeURIComponent,相应3个解码函数:unescape,decodeURI,decodeURIComponen

    Delphi Base64编码,javascript解码汉字乱码解决方法演示程序(源代码)

    解决思路:进行Base64前先进行URL编码,在进行URL编码的时候,注意设置不需要SpaceAsPlus选项。 javascript代码: let decodedData = window.atob(JSONStr); let decodedData1 = decodeURIComponent(decodedData)...

    javascript url几种编码方式详解

    2. encodeURI()是javascript中真正用来对URL编码的函数。编码整个URL地址,但对特殊含义的符号”;/?:@&=+$,#”,也不进行编码。对应的解码函数是decodeURI()。 3. encodeURIComponent()能编码”;/?:@&=+$,#”这些...

    JavaScript、C# URL编码、解码总结

    JavaScript部分 encodeURI() (解码为:decodeURI()):不会转义的字符:- _ . ! ~ * ‘ ( ) ;/?:@&=+$,# 例如: encodeURI(“//www.jb51.net?a=-_.!~*'();/?:@&=+$,#”) 输出: “//www.jb51.net?a=-_.!~*'();/?:@...

    关于JAVASCRIPT urldecode URL解码的问题

    这个时候,出现了encodeURIComponent、decodeURIComponent,它可以完全的对URL进行编码解码,但是遇到例如搜索引擎用到的部分转码,又摸不到门了,没问题,PHP官方出了一个解决方案: 代码如下: decodeURIComponent...

    node-iconv-urlencode:用于将 url 编码的字符串编码和解码为每种可能编码的节点包

    iconv-urlencode 用于编码和解码来自/到每种可能编码的 url 编码字符串的节点包这个包使用iconv-lite进行不同字符集的编码。 因此,此包支持的所有编码都可以在这里使用。 转换后的字符串根据 HTML5 规范进行 url ...

    JS前端知识点 运算符优先级,URL编码与解码,String,Math,arguments操作整理总结

    主要介绍了JS前端知识点 运算符优先级,URL编码与解码,String,Math,arguments操作,结合实例形式整理总结了javascript运算符优先级,URL编码与解码,String,Math,arguments操作原理及使用技巧,需要的朋友可以...

    JavaScript中的编码和解码函数

    js对文字进行编码涉及3个函数:escape,encodeURI,encodeURIComponent,相应3个解码函数:unescape,decodeURI,decodeURIComponent 1、 传递参数时需要使用encodeURIComponent,这样组合的url才不会被#等特殊字符截断...

    base64url:用于编码和解码base64url!

    base64url 往返 安装 $ npm install base64url 使用npm安装后,您可以从JavaScript或TypeScript要求此库: ...base64url编码input 。 输入应该是string或Buffer 。 例子 &gt; base64url ( "ladies and g

    各种形式的编码解码工具

    收集整理了各种通过JAVASCRIPT编码解码工具 支持转换的有 \uXXXX \UXXXXXXXX &#DDDD; &#xXXXX; \xXX \OOO Base64 Quoted-printable MIME + Base64 MIME + Quoted-printable 如输入:测试CSDN 结果为: 测试CSDN \...

    支持中文和urlsafe编码的Base64编解码库

    虽然JavaScript中可以使用原生的btoa和atob函数进行Base64的编解码。但是不支持中文字符,并且不支持url-safe的Base64编解码。当编码后的结果要是通过get请求传输时(比如跨域提交时),结果中包含有'/'字符将导致...

    compressor:Javascript有效编码,将数字编码到Base64 URL

    将Javascript数字有效编码到Base64 URL介绍Compressor是用于数字数组的Javascript编码器/解码器。 它将数字编码为有效的字符串。输入[ 5 , 0 , 5 , 6 , 3 , 4 , 5 , 0 , 5 , 6 ] ;输出"Coucou" ; Compressor可以编码...

    详谈js对url进行编码和解码(三种方式的区别)

    实际上,escape()不能直接用于URL编码,它的真正作用是返回一个字符的Unicode编码值。比如”春节”的返回结果是%u6625%u8282,也就是说在Unicode字符集中,”春”是第6625个(十六进制)字符,”节”是第8282个...

    postman-url-encoder:根据WHATWG规范实现URL编码

    邮递员URL编码器 邮递员URL编码器是一个NodeJS模块,提供了各种与URL编码相关的API。 创建该模块是为了实现以消除跨Postman系统对Node URL API的依赖。 这些API可用于编码URL的不同部分(例如主机名,路径,查询)...

    hex64:Base64编码和解码URL的UTF-8字符串(模块和二进制工具)

    Base64编码/解码URL的十六进制字符串(模块和二进制工具) 安装: npm install -g hex64 命令行: $ hex64 b64 cccccc zMzM $ hex64 hex zMzM cccccc $ hex64 cccccc b64: zMzM $ hex64 zMzM hex: cccccc $ hex64 ...

Global site tag (gtag.js) - Google Analytics