escape()

Don't use it, as it has been deprecated since ECMAScript v3.

encodeURI()

Use encodeURI when you want a working URL. Make this call:

encodeURI("http://www.google.com/a file with spaces.html")

to get:

http://www.google.com/a%20file%20with%20spaces.html

Don't call encodeURIComponent since it would destroy the URL and return

http%3A%2F%2Fwww.google.com%2Fa%20file%20with%20spaces.html

encodeURIComponent()

Use encodeURIComponent when you want to encode a URL parameter.

param = encodeURIComponent('http://xyz.com/?a=12&b=55')
url = 'http://domain.com/?param=' + param ;
And you will get this complete URL:

http://www.domain.com/?param=http%3A%2F%2Fxyz.com%2F%Ffa%3D12%26b%3D55

Note that encodeURIComponent does not escape the ' character.

A common bug is to use it to create html attributes such as href='MyUrl', which could suffer an injection bug.

If you are constructing html from strings, either use " instead of ' for attribute quotes, or add an extra layer of encoding (' can be encoded as %27).

For more information on this type of encoding you can check: http://en.wikipedia.org/wiki/Percent-encoding

Best practice: escape, or encodeURI / encodeURIComponent的更多相关文章

  1. js中的三个编码函数:escape,encodeURI,encodeURIComponent

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

  2. escape()、encodeURI()、encodeURIComponent()区别详解--zt

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

  3. JS转义 escape()、encodeURI()、encodeURIComponent()区别详解

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

  4. javascript之url转义escape()、encodeURI()和encodeURIComponent()

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

  5. escape()、encodeURI()、encodeURIComponent()区别详解

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

  6. escape()、encodeURI()、encodeURIComponent() difference

    escape() 方法: 采用ISO Latin字符集对指定的字符串进行编码.所有的空格符.标点符号.特殊字符以及其他非ASCII字符都将被转化成%xx格式的字符编码(xx等于该字符在字符集表里面的编 ...

  7. JS 字符串编码函数(解决URL特殊字符传递问题):escape()、encodeURI()、encodeURIComponent()区别详解

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

  8. JS escape、encodeURI 、encodeURIComponent 编码与解码[转]

    转至:http://jc-dreaming.iteye.com/blog/1702407 本文讨论如何对传递参数用JS编码与解码 1:编码与解码方法的对应关系 escape ------------- ...

  9. 比较escape、encodeURI、encodeURIComponent

    估计很多前端工程师并不清楚escape,encodeURI, encodeURIComponent的区别,也不知道什么时候该用哪个方法,以及这些方法为什么要被用到,下面我主要来阐述一下这三个方法的区别 ...

随机推荐

  1. 2016.6.11 ASP提交数据到SQL server数据乱码解决方法

    1.检查数据库排序规则 China-PRE-90-CS-AI 2.ASP文档中,写入数据的页面的编码和检查提交数据页面的编码一致:

  2. js:语言精髓笔记7----原型继承

    面向对象有三个基本特性:封装,继承,多态:如果都满足的话称为面向对象语言:而部分满足则称为基于对象语言: 数据类型实现模型描述: JavaScript对象模型: 构造过程:函数->构造器 构造器 ...

  3. BZOJ4013 : [HNOI2015]实验比较

    首先用并查集将等号缩点,然后拓扑排序判断有没有环,有环则无解,否则通过增加超级源点$0$,可以得到一棵树. 设$f[x][y]$表示$x$子树里有$y$种不同的数字的方案数,由底向上DP. 对于当前点 ...

  4. BZOJ1077 : [SCOI2008]天平

    首先通过差分约束系统建图,用Floyed算法求出任意两个砝码差值的上下界. 然后暴力枚举放在右边的砝码C,D,通过与A,B差值的上下界分类讨论统计方案. 时间复杂度$O(N^3)$. #include ...

  5. robotium 新建 android 测试项目:

    注意:新建项目后再运行前一定要修改Manifest文件中的instrumentation 中的target package, 这个是测试的入口 1. 程序开始要通知系统我要测的app是什么 如何知道a ...

  6. UVA 10325 - The Lottery(容斥)

    以前做过的一个题,忘记/gcd了,看来需要把以前的东西看一下啊. #include <cstdio> #include <cstring> #include <iostr ...

  7. Odoo Two ways to pop warning infomation

    1. raise ValueError(_('title'),_('message')) 2.raise except_orm(_('title'),_('message'))

  8. 在Eclipse中配置Tomcat 创建和运行Servlet/JSP

    在Eclipse中配置Tomcat 创建和运行Servlet/JSP 步骤一:在Eclipse中配置Tomcat(注意下载Eclipse IDE for Java EE Developers) (1) ...

  9. 不用逗号进行UNION

    ./download.php ...if($link){    //加密后ID$link = base64_decode(urldecode($link));$link_array = explode ...

  10. [转]Calling Web Service Functions Asynchronously from a Web Page 异步调用WebServices

    本文转自:http://www.codeproject.com/Articles/70441/Calling-Web-Service-Functions-Asynchronously-from Ove ...