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

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

encodeURIComponent不编码字符有71个:!, ‘,(,),*,-,.,_,~,0-9,a-z,A-Z

1.encodeURI

encodeURI是用来编码URI的,最常见的就是编码一个URL。 encodeURI会将需要编码的字符转换为UTF-8的格式。decodeURI方法可以恢复到原有字符串。

encodeURI方法不会编码下列字符:":", "/", ";", and "?",不过我们可以通过下面的encodeURIComponent方法来编码这些字符。

例如URL中包含中文:

encodeURI('http://www.我.com')   // => "http://www.%E6%88%91.com"

由于encodeURI不转义&+, 和 =。所以URL参数的值是无法转义的,比如我们想把a=?传给服务器:

encodeURI('http://www.我.com?a=?')   // => "http://www.%E6%88%91.com?a=?"

服务器收到的a值为空,因为?是URL保留字符。此时我们需要用encodeURIComponent来编码!

2.encodeURIComponent

encodeURIComponent是用来编码URI参数的。decodeURIComponent方法可以恢复到原有字符串。

它只会跳过非转义字符(字母数字以及-_.!~*'()), URL保留字符(;,/?:@&=+$#)均会被转义。

由于encodeURIComponent能够编码差不多所有字符,当我们把编码过的/folder1/folder2/default.html发送到服务器时时,由于‘/’也将被编码,服务器将无法正确识别。

比如上面的例子:

// => "http://www.我.com?a=%3F"
encodeURI('http://www.我.com') + '?a=' + encodeURIComponent('?');

因为encodeURIComponent会编码所有的URL保留字,所以不适合编码URL,例如:

encodeURIComponent('http://www.我.com')
"http%3A%2F%2Fwww.%E6%88%91.com"

3.btoa

btoa:将ascii字符串或二进制数据转换成一个base64编码过的字符串,该方法不能直接作用于Unicode字符串。

atob:将已经被base64编码过的数据进行解码。

注意:因为btoa仅将ascii字符串或二进制数据进行编码,不能作用于unicode字符串,所以对中文的base64编码会报错:

btoa("hello 童童");
// InvalidCharacterError: 'btoa' failed: The string to be encoded contains characters outside of the Latin1 range.

如果要对中文进行base64编码,只需要将中文进行 encodeURIComponent 进行编码之后再进行 base64编码即可。

btoa(encodeURIComponent("hello 童童"));
// "aGVsbG8lMjAlNDAlRTQlQkElOTElRTYlQjclQTElRTclODQlQjY="

完整的utf8编码字符串进行base64编码示例:

// 完整的utf8字符串base64编码与解码
function uft8ToBase64(utf8) {
return btoa(encodeURIComponent(utf8));
}
function base64ToUtf8(base64) {
return decodeURIComponent(atob(base64));
}
var base64 = uft8ToBase64("hello 童童");
// "aGVsbG8lMjAlNDAlRTQlQkElOTElRTYlQjclQTElRTclODQlQjY="
base64ToUtf8(base64);
// "hello 童童"

encodeURI、encodeURIComponent、btoa及其应用场景的更多相关文章

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

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

  2. escape,encodeURI,encodeURIComponent函数比较

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

  3. escape,encodeURI,encodeURIComponent

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

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

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

  5. URI 方法 encodeURI() encodeURIComponent() docodeURI() decodeURIComponent()

    URI 方法  encodeURI()  encodeURIComponent()  docodeURI()  decodeURIComponent()   var sUri = “http://ww ...

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

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

  7. encodeURI & encodeURIComponent

    [encodeURI & encodeURIComponent]  区别在于,"&", "+", 和 "=" 不会被enco ...

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

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

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

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

随机推荐

  1. visualstudio 2013 mysql entityframework :实体模型无法添加,闪退

    发现电脑中安装的mysql-connector-net,版本为6.9.8 1.卸载此版本 2.重新安装mysql-connector-net 6.8.3 3.注意web.config中版本 4.注意项 ...

  2. ZOJ2748 Free Kick 2017-04-18 20:40 40人阅读 评论(0) 收藏

    Free Kick Time Limit: 2 Seconds      Memory Limit: 65536 KB In a soccer game, a direct free kick is ...

  3. ZOJ3767 Elevator 2017-04-13 23:32 37人阅读 评论(0) 收藏

    Elevator Time Limit: 2 Seconds      Memory Limit: 65536 KB How time flies! The graduation of this ye ...

  4. 两种步骤 更改 EBS R12界面LOGO以及内容

    from:metalink more: Note 174219.1 - How To Change The Logo In The Oracle Application Menu Note 84975 ...

  5. [leetcode] 6. Balanced Binary Tree

    这个题目纠结了一会儿,终于从二叉树转化到AVL了.题目如下: Given a binary tree, determine if it is height-balanced. For this pro ...

  6. Mysql部署

    1. 下载 Mysql 版本为: mysql-5.6.43-linux-glibc2.12-x86_64.tar.gz (注意:下载二进制文件)  存放位置: /usr/local 2. 检查机器上 ...

  7. Ubuntu 16.04 无人值守自动更新

    https://help.ubuntu.com/lts/serverguide/automatic-updates.html 设置说明 APT::Periodic::Update-Package-Li ...

  8. Linux socat轻松实现TCP/UDP端口转发

    1.TCP端口转发 socat -d TCP4-LISTEN:,reuseaddr,fork TCP4: 2.UDP端口转发 socat -T UDP4-LISTEN:,reuseaddr,fork ...

  9. log4net 日志打印不全

    程序用的是log4net打印日志,偶现日志打印不全的问题,程序的log4net配置如下: <log4net> <root> <level value="ALL& ...

  10. C# PowerPoint操作的基本用法。

    代码using System;using System.Collections.Generic;using System.Linq;using System.Text;using OFFICECORE ...