`
guanchenglong0220
  • 浏览: 17582 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

JS URL编码转换函数

阅读更多
JS URL编码转换函数

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.



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

1、   传递参数时需要使用encodeURIComponent,这样组合的url才不会被#等特殊字符截断。                           
例如:<script language="javascript">document.write('<a href="http://passport.baidu.com/?logout&aid=7&u='+encodeURIComponent("http://cang.baidu.com/bruce42")+'">退出</a>');</script>

2、   进行url跳转时可以整体使用encodeURI

例如:Location.href=encodeURI("http://cang.baidu.com/do/s?word=百度&ct=21");

3、   js使用数据时可以使用escape

[Huoho.Com编辑]

例如:搜藏中history纪录。

4、   escape对0-255以外的unicode值进行编码时输出%u****格式,其它情况下escape,encodeURI,encodeURIComponent编码结果相同。


最多使用的应为encodeURIComponent,它是将中文、韩文等特殊字符转换成utf-8格式的url编码,所以如果给后台传递参数需要使用encodeURIComponent时需要后台解码对utf-8支持(form中的编码方式和当前页面编码方式相同)

escape不编码字符有69个:*,+,-,.,/,@,_,0-9,a-z,A-Z

encodeURI不编码字符有82个:!,#,$,&,',(,),*,+,,,-,.,/,:,;,=,?,@,_,~,0-9,a-z,A-Z

encodeURIComponent不编码字符有71个:!, ',(,),*,-,.,_,~,0-9,a-z,A-Z
分享到:
评论

相关推荐

    unicode格式的字符串进行URL编码

    js函数把base64编码还原成unicode的数据 然后直接调用URL编码函数对数据进行编码返回。就得到易语言支持的url编码unicode字节集的结果。有可能的话我建议精易模块也收录一下或者做一下这方便的URL编码。@龙卷风暴。

    易语言-unicode格式的字符串进行URL编码

    js函数把base64编码还原成unicode的数据 然后直接调用URL编码函数对数据进行编码返回 就得到易语言支持的url编码unicode字节集的结果 有可能的话我建议精易模块也收录一下或者做一下这方便的URL编码

    unicode格式的字符串进行URL编码-易语言

    今天写一个POST程序的时候有一段UNICODE字符串需要进行URL编码后进行提交 ...js函数把base64编码还原成unicode的数据 然后直接调用URL编码函数对数据进行编码返回 就得到易语言支持的url编码unicode字节集的结果

    javascript URL编码和解码使用说明

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

    JS版 UrlEncode

    JS版 UrlEncode 和 UrlDecode 函数

    asp.net URL 显示乱码 解决方法

    比如你传入汉字,或者传入”§”等其他编码格式的...编码方法可以写到js文件中使用encodeURIComponent(source)函数,其中source为中文。可以将特殊字符进行转换,被转换后的url就不会出现乱码了。 您可能感兴趣的文章

    JavaScript中的编码和解码函数

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

    JS中encodeURIComponent函数用php解码的代码

    就可以得到你需要的字串了,其中gb2312根据你实际应用来定如还不明白为什么看下面的文章 URL编码转换,escape() encodeURI() encodeURIComponent() 本文介绍对url编码的三种函数 escape() ,encodeURI

    js中编码函数:escape,encodeURI与encodeURIComponent详解

    1、eacape(): 该方法不会对 ASCII 字母和数字进行编码,也不会对下面这些 ASCII 标点符号进行编码: * @ – _ + . / 。其他所有的字符都会被转义序列替换。其它情况下escape,encodeURI,encodeURIComponent编码结果...

    js中escape对应的C#解码函数 UrlDecode

    js中escape对应的C#解码函数 System.Web.HttpUtility.UrlDecode(s) //注意编码 需要注意的几点: 1、HttpUtility.UrlEncode,HttpUtility.UrlDecode是静态方法,而Server.UrlEncode,Server.UrlDecode是实例方法。...

    Javascript下的urlencode编码解码方法附decodeURIComponent

    而相信碰到过此问题的朋友应该都有所了解,目前网络上流行一些js下的自定义函数去解决这个问题,如说vbscript(URLDecode())、javascript(UrlDecode())等。而这两个函数,都无法很好的与asp(Server.Ur

    将字符串转换成gb2312或者utf-8编码的参数(js版)

    下面我们来介绍一下方法 1、我们新建一个 UrlEncode.js 然后将下面的代码拷贝进去 代码如下: //JS版的Server.UrlEncode编码函数 String.prototype.UrlEncodeGB2312 = function () { var str = this; str = str....

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

    89、原生JavaScript实现金额大写转换函数 90、原生JavaScript常用的正则表达式大收集 91、原生JavaScript实现窗体改变事件resize的操作(兼容所以的浏览器) 92、原生JavaScript用正则清除空格分左右 93、原生...

    js编码之encodeURIComponent使用介绍(asp,php)

    在对新的URL编码的时候发现,网页编码的格式对于JS的影响很大,在这里书写一点。 {var B=siteUrl+this.actionUrl+(this.type===”all”?””:this.type)+”&SearchWord=”+encodeURIComponent(A).replace(/’/g,”'...

    JavaScript详解(第2版)

     13.7.4 将事件传递给JavaScript函数   13.7.5 鼠标坐标   13.7.6 按键事件   13.8 处理事件的脚本模型   13.9 应知应会   练习   第14章 CSS与JavaScript   14.1 什么是CSS   14.2 什么是...

    javascript入门笔记

    Javascript,简称为 JS,是一款能够运行在 JS解释器/引擎 中的脚本语言 JS解释器/引擎 是JS的运行环境: 1、独立安装的JS解释器 - NodeJS 2、嵌入在浏览器中的JS解释器 JS的发展史: 1、1992年 Nombas 开发...

    JavaScript笔记

    7.数据类型转换函数 :(方法前不需要对象调用的:全局函数) |--toString():转换成字符串。所有数据类型均可转换为 string 类型; |--parseInt():强制转换成整数。如果不能转换,则返回 NaN(not a number); ...

    JavaScript基础教程第8版

    《JavaScript基础教程(第8版)》循序渐进地讲述了JavaScript及相关的CSS、DOM、Ajax、jQuery等技术。书中从JavaScript语言基础开始,分别讨论了图像、框架、浏览器窗口、表单、正则表达式、用户事件和cookie,并在上...

    程序天下:JavaScript实例自学手册

    第1章 页面特效 ...1.2 页面自动最大化 1.3 页面自动刷新 1.4 页面的后退、刷新、前进 1.5保护网页源代码 1.6 保护自己的网页不被放入框架 1.7 保护自己的网页不被放入...22.13 用JavaScript实现编码解码 22.14 创建带属性...

    《程序天下:JavaScript实例自学手册》光盘源码

    第1章 页面特效 ...1.2 页面自动最大化 ...22.13 用JavaScript实现编码解码 22.14 创建带属性的对象 22.15 用prototype实现JavaScript的继承 22.16 JavaScript制作哈希表 第23章 其他技巧及特效 23.1 ...

Global site tag (gtag.js) - Google Analytics