• escape
    unescape

  • encodeURI
    decodeURI

  • encodeURIComponent
    decodeURIComponent

这六个方法功能有关联,如果不清楚每一个的作用,很容易混淆。问题的本质,是如何在 URL 中正确处理各种令人头疼的字符

首先,escape unescape 已经废弃,应当避免使用。

  • The deprecated escape() method computes a new string in which certain characters have been replaced by a hexadecimal escape sequence. Use encodeURI or encodeURIComponent instead.

  • The deprecated unescape() method computes a new string in which hexadecimal escape sequences are replaced with the character that it represents. The escape sequences might be introduced by a function like escape. Because unescape is deprecated, use decodeURI or decodeURIComponent instead.

根据 MDN 的说明,escape 应当换用为 encodeURI 或 encodeURIComponent;unescape 应当换用为 decodeURI 或 decodeURIComponent。

那么,问题就简化为 encodeURI decodeURI 与encodeURIComponent decodeURIComponent 的区分。encodeURI 应当用于整个 URI 的编码,encodeURIComponent 应当用于 URI 中某个部分的编码。

如果用 URL 举例,如下:

encodeURI('https://www.baidu.com/ a b c')
// "https://www.baidu.com/%20a%20b%20c"
encodeURIComponent('https://www.baidu.com/ a b c')
// "https%3A%2F%2Fwww.baidu.com%2F%20a%20b%20c"

而 escape 会编码成下面这样,不伦不类,所以废弃。

escape('https://www.baidu.com/ a b c')
// "https%3A//www.baidu.com/%20a%20b%20c"

由此可知,前端开发中用到最多的应该是 encodeURIComponent/decodeURIComponent。例如下面这个将 URL Search 中的参数转化为对象的方法:

var parseUrlSearch = function() {
var query = window.location.search.slice(1);
var result = {};
query.split("&").forEach(function(part) {
var item = part.split("=");
result[item[0]] = decodeURIComponent(item[1]);
});
return result;
};

作者:天方夜
链接:https://www.zhihu.com/question/21861899/answer/62546831
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

转载:escape,encodeURI,encodeURIComponent有什么区别?的更多相关文章

  1. 【转】escape,encodeURI,encodeURIComponent有什么区别?

    在这个页面里面试着搜了一下 「UTF-8」 ,居然没有搜到. escape 和 encodeURI 都属于 Percent-encoding,基本功能都是把 URI 非法字符转化成合法字符,转化后形式 ...

  2. escape,encodeURI,encodeURIComponent 之间的区别和使用

    escape(目前已经被淘汰)是对字符串(string)进行编码(而另外两种是对URL),不会对下列字符编码 ASCII字母  数字  @*/+ 最关键的是,当你需要对URL编码时,请忘记这个方法,这 ...

  3. Flex中escape/encodeURI/encodeURIComponent的区别

    Flex中提供了三种转码函数,各有各的区别, escape,encodeURI,encodeURIComponent 这三个函数不仅在flex中有道运用在javascript中同样的含义 ,今天我仔细 ...

  4. JavaScript中有三个可以对字符串编码的函数,分别是: escape(),encodeURI(),encodeURIComponent()

    JavaScript中有三个可以对字符串编码的函数,分别是: escape,encodeURI,encodeURIComponent,相应3个解码函数:unescape,decodeURI,decod ...

  5. url的三个js编码函数escape(),encodeURI(),encodeURIComponent()简介

    url的三个js编码函数escape(),encodeURI(),encodeURIComponent()简介 2014年10月12日 16806次浏览 引子 浏览器URl地址,上网一定会用到,但是浏 ...

  6. escape,encodeURI,encodeURIComponent

    JavaScript/js中,有三个可以对字符串编码的函数,分别是: escape,encodeURI,encodeURIComponent,相应3个解码函数:unescape,decodeURI,d ...

  7. url的三个js编码函数escape(),encodeURI(),encodeURIComponent()简介【转】

    引子 浏览器URl地址,上网一定会用到,但是浏览器地址有中文或者浏览器url参数操作的时候,经常会用到encodeURIComponent()和decodeURIComponent()以及encode ...

  8. JavaScript中有对字符串编码的三个函数:escape,encodeURI,encodeURIComponent

    JavaScript中有三个可以对字符串编码的函数,分别是: escape,encodeURI,encodeURIComponent,相应3个解码函数:unescape,decodeURI,decod ...

  9. escape,encodeURI,encodeURIComponent函数比较

    escape,encodeURI,encodeURIComponent函数比较 js对文字进行编码涉及3个函数:escape,encodeURI,encodeURIComponent,相应3个解码函数 ...

随机推荐

  1. linux以16进制查看文件

    vim 先用vim -b data 以2进制打开文件,然后用xxd工具转化,在vim的命令行模式下: :%!xxd        --将当前文本转化为16进制格式 :%!xxd -r    --将16 ...

  2. 面向对象select方法

    <?php class Table{         protected $tablename;         protected $arrTable;        protected $w ...

  3. 1.1:Get Started with Unity Shaders

    文章著作权归作者所有.转载请联系作者,并在文中注明出处,给出原文链接. 本系列原更新于作者的github博客,这里给出链接. 第1章开始正式进入Unity Shader的学习. 什么是Shader 本 ...

  4. Docker Swarm 创建服务

    Docker Swarm 创建服务 环境: 系统:Centos 7.4 x64 应用版本:Docker 18.09.0 管理节点:192.168.1.79 工作节点:192.168.1.78 工作节点 ...

  5. Python sqlalchemy orm 多对多外键关联

    多对多外键关联 注:使用三张表进行对应关联 实现代码: # 创建3个表 配置外键关联 # 调用Column创建字段 加类型 from sqlalchemy import Table, Column, ...

  6. shell爬虫--抓取某在线文档所有页面

    在线教程一般像流水线一样,页面有上一页下一页的按钮,因此,可以利用shell写一个爬虫读取下一页链接地址,配合wget将教程所有内容抓取. 以postgresql中文网为例.下面是实例代码 #!/bi ...

  7. Python爬虫(四)——豆瓣数据模型训练与检测

    前文参考: Python爬虫(一)——豆瓣下图书信息 Python爬虫(二)——豆瓣图书决策树构建 Python爬虫(三)——对豆瓣图书各模块评论数与评分图形化分析 数据的构建 在这张表中我们可以发现 ...

  8. nrm 安装与使用

    1.使用 npm install nrm -global 全局安装 2.安装完成后使用 nrm ls命令查看其维护的镜像地址列表 3.* 星号表示在使用 npm下载资源的时候,默认使用的地址 这里需要 ...

  9. 关于 使用python向qq好友发送消息(对爬虫的作用----当程序执行完毕或者报错无限给自己qq发送消息,直到关闭)

    以前看到网上一些小程序,在处理完事物后会自动发送qq消息,但是一直搞不懂是说明原理.也在网上找过一些python登陆qq发送消息的文字,但是都太复杂了.今天偶然看到一篇文章,是用python调用win ...

  10. AJAX缓存清理

    Ajax页面缓存是ajax处理数据时对一些重复相同数据进行一个缓存操作,这样从另一个层面对于我们来讲是非常的不错了,但有时我们并不希望它缓存要如何处理呢?下面我们一起来看看关于页面缓存问题分析与解决, ...