`
ld_hust
  • 浏览: 166039 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

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

分享到:
评论

相关推荐

    纯Javascript脚本实现GBK URL编解码

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

    javascript 对url编码 解码

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

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

    Delphi进行Base64编码后,如果使用javascript解码,会出现如下问题: 1. 汉字乱码 2. 空格变成 + 号 本资源演示Delphi代码,实现javascript能正确进行Base64解码,不出现上述2个问题。 解决思路:进行Base64前先...

    关于JAVASCRIPT urldecode URL解码的问题

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

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

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

    javascript url几种编码方式详解

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

    javascript 三种编解码方式

    1、 传递参数时需要使用encodeURIComponent,这样组合的url才不会被#等特殊字符截断。 例如:[removed][removed](‘退出’);[removed] 2、 进行url跳转时可以整体使用encodeURI 例如:Location.href=encodeURI(...

    javascript URL编码和解码使用说明

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

    URL 解码器

    URL 解码器 网页版,单个html文件,其中的脚本全部为javascript 支持GB2312、Unicode、UTF-8 简洁实用 PS:其中的js代码收集自网络

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

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

    Javascript UrlDecode函数代码

    将Url进行编码,前台JS需要使用这段内容,这时候就需要解码了

    opus-stream-decoder:使用JavaScript和WebAssembly(Wasm)即时解码Ogg Opus音频块

    OpusStreamDecoder是Emscripten JavaScript WebAssembly(Wasm)库,用于立即解码成块的Ogg Opus音频流(URL或文件),而无需等待完整的文件下载,复制或读取。 是用于解码的基础C库。 OpusStreamDecoder提供了轻量...

    100个直接可以拿来用的JavaScript实用功能代码片段(1-10)

    24、原生JavaScript检验URL链接是否有效 25、原生JavaScript格式化CSS样式代码 26、原生JavaScript压缩CSS样式代码 27、原生JavaScript获取当前路径 28、原生JavaScriptIP转成整型 29、原生JavaScript整型解析为IP...

    DecodeShSt:使用JavaScript解码sh.st URL

    #DecodeShSt使用javascript解码sh.st网址。 用法 解码sh.st URL: ./sh.st.js {sh.stUrl} 网址中必须包含“ http://”,否则将无法使用。 sh.st由于只是一个简单的GET请求,因此可以自动将您重定向,而无需等待5...

    用JavaScript实现PHP的urldecode/urldecode函数

    这个是 utf-8版本的 js实现 php的 urlencode() 和 urldecode()两个函数的功能。 在传送cookie的时候 在 php端实现 url编码 但要用 js来解码cookie的时候 就出现汉字不能不能识别的问题 这个 js 很好的解决

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

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

Global site tag (gtag.js) - Google Analytics