1. 关于URL、encodeURI 及 encodeURIComponent:

URI: Uniform Resource Identifiers,通用资源标识符

Global 对象的 encodeURI() 和 encodeURIComponent() 方法可以对 URI 进行编码,以便发送给浏览器。

有效的 URI 中不能包含某些字符,例如空格。而这 URI 编码方法就可以对 URI 进行编码,它们用特殊的 UTF-8 编码替换所有无效的字符,从而让浏览器能够接受和理解。

其中 encodeURI() 主要用于整个 URI (例如,http://www.jxbh.cn/illegal value.htm),而 encode-URIComponent() 主要用于对 URI 中的某一段(例如前面 URI 中的 illegal value.htm)进行编码。

它们的主要区别在于,encodeURI() 不会对本身属于 URI 的特殊字符进行编码,例如冒号、正斜杠、问号和 # 字号;而 encodeURIComponent() 则会对它发现的任何非标准字符进行编码。来看下面的例子:

var uri="http://www.jxbh.cn/illegal value.htm#start";
//”http: //www.jxbh.cn/illegal%20value .htm#s tart”
alert(encodeURI (uri)):
//”http% 3A%2F%2Fwww.jxbh.cn%2 Fillegal%2 0value. htm%23 start”
alert( encodaURIComponent (uri));

使用 encodeURI() 编码后的结果是除了空格之外的其他字符都原封不动,只有空格被替换成了 %20。而 encodeURIComponent() 方法则会使用对应的编码替换所有非字母数字字符。这也正是可以对整个 URI 使用 encodeURI(),而只能对附加在现有 URI 后面的字符串使用 encodeURIComponent() 的原因所在。一般来说,我们使用 encodeURIComponent() 方法的时候要比使用 encodeURI() 更多,因为在实践中更常见的是对查询字符串参数而不是对基础 URL 进行编码。

2. 关于 decodeURI、decodeURIComponent:

了解了 encodeURI 与 encodeURIComponent 的区别后,我们来看下 decodeURI 与 decodeURIComponent。

总结:最好使用 decodeURIComponent() 进行解码。

防止中文乱码方法: decodeURIComponent(数据,true)

3. 一个小考题:

// 请解码被多次编码的 URL
例子:https%253A%252F%252Fwww.baidu.com%252F%253Ftest%253D1
条件:被编码次数未知 结果:https://www.baidu.com/?test=1

decodeURI 与 decodeURIComponent 区别的更多相关文章

  1. js解码编码decodeURI与decodeURIComponent区别

    ###decodeURI与decodeURIComponent区别 1. 概念: URI: Uniform ResourceIdentifiers,通用资源标识符 Global对象的encodeURI ...

  2. JS中decodeURI()与decodeURIComponent()区别

    decodeURI()定义和用法:decodeURI() 函数可对 encodeURI() 函数编码过的URI 进行解码. 语法:decodeURI(URIstring) 参数 描述:URIstrin ...

  3. 一张图看懂encodeURI、encodeURIComponent、decodeURI、decodeURIComponent的区别

    一.这四个方法的用处 1.用来编码和解码URI的 统一资源标识符,或叫做 URI,是用来标识互联网上的资源(例如,网页或文件)和怎样访问这些资源的传输协议(例如,HTTP 或 FTP)的字符串.除了e ...

  4. encodeURI、encodeURIComponent、decodeURI、decodeURIComponent的区别

    一.这四个方法的用处 1.用来编码和解码URI的 统一资源标识符,或叫做 URI,是用来标识互联网上的资源(例如,网页或文件)和怎样访问这些资源的传输协议(例如,HTTP 或 FTP)的字符串.除了e ...

  5. 结合实例详细介绍encodeURI()、encodeURIComponent()、decodeURI()、decodeURIComponent()使用方法

    在介绍encodeURI().encodeURIComponent().decodeURI().decodeURIComponent()方法前我们需要了解Global对象的概念:   Global(全 ...

  6. javascript中escape()、unescape()、encodeURI()、encodeURIComponent()、decodeURI()、decodeURIComponent()比较

    这些URI方法encodeURI.encodeURIComponent().decodeURI().decodeURIComponent()代替了BOM的escape()和unescape()方法.U ...

  7. decodeURI、decodeURIComponent 编码方法

    ——摘自<JavaScript高级程序设计> 编码: Global 对象的 encodeURI()和 encodeURIComponent()方法可以对 URI(Uniform Resou ...

  8. JS中encodeURI、encodeURIComponent、decodeURI、decodeURIComponent

    js 对文字进行编码涉及2个函数:encodeURI,encodeURIComponent,相应2个解码函数:decodeURI,decodeURIComponent 1.用来编码和解码URI的 统一 ...

  9. JavaScript decodeURI()与decodeURIComponent() 使用与区别

    decodeURI()定义和用法:decodeURI()函数可对encodeURI()函数编码过的URI进行解码.语法:decodeURI(URIstring)参数描述:URIstring必需,一个字 ...

随机推荐

  1. 暑期ACM集训

    2019-07-17 08:42:11 这是总结昨天的做题情况 总体来说,好久的没做题了,实力下降了许多,这一个月假又学习吧!!!! A - Ropewalkers Polycarp decided ...

  2. golang 上传文件(包括 gin 实现)

    golang web服务有时候需要提供上传文件的接口,以下就是具体示例.为了示例简单(吐槽下 golang 的错误处理), 忽略了所有的错误处理.本文会用两种方式(标准库和gin)详细讲解 golan ...

  3. Nginx 配置反向代理ip

    参考文档: https://blog.csdn.net/stevenprime/article/details/7918094

  4. IDEA 导入 NodeJS 项目部署启动

    1.导入项目 2.添加模块 3.配置启动项 4.启动 5.备注 如果不明白,新建一个项目查看配置详情 原文地址:https://blog.csdn.net/tiankongzhichenglyf/ar ...

  5. App功能测试点总结

    1.手机操作系统android(谷歌).ios(苹果).Windows phone(微软).Symbian(诺基亚).BlackBerry OS(黑莓).windows mobile(微软),目前主流 ...

  6. net技术方案

    https://www.cnblogs.com/qyq0323/p/11765243.html https://yq.aliyun.com/articles/300527

  7. python——操作系统的发展史

    一.手工操作 —— 穿孔卡片 1946年第一台计算机诞生--20世纪50年代中期,计算机工作还在采用手工操作方式.此时还没有操作系统的概念.    程序员将对应于程序和数据的已穿孔的纸带(或卡片)装入 ...

  8. centos7上使用git clone出现问题

    centos 7  git clone时出现不支持协议版本的问题 unable to access 'https://github.com/baloonwj/TeamTalk.git/': Peer ...

  9. Eclipse workspace被锁定

    重新打开Eclipse时,提示如下: Workspace Unavailable: Workspace in use or cannot be created, choose a different ...

  10. pycharm中文乱码

    python2默认不支持中文,python3支持中文,所以使用python2要注意. 解决方案: 顶部声明一下是utf8编码即可,   # encoding=utf8