decodeURI 与 decodeURIComponent 区别
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 区别的更多相关文章
- js解码编码decodeURI与decodeURIComponent区别
###decodeURI与decodeURIComponent区别 1. 概念: URI: Uniform ResourceIdentifiers,通用资源标识符 Global对象的encodeURI ...
- JS中decodeURI()与decodeURIComponent()区别
decodeURI()定义和用法:decodeURI() 函数可对 encodeURI() 函数编码过的URI 进行解码. 语法:decodeURI(URIstring) 参数 描述:URIstrin ...
- 一张图看懂encodeURI、encodeURIComponent、decodeURI、decodeURIComponent的区别
一.这四个方法的用处 1.用来编码和解码URI的 统一资源标识符,或叫做 URI,是用来标识互联网上的资源(例如,网页或文件)和怎样访问这些资源的传输协议(例如,HTTP 或 FTP)的字符串.除了e ...
- encodeURI、encodeURIComponent、decodeURI、decodeURIComponent的区别
一.这四个方法的用处 1.用来编码和解码URI的 统一资源标识符,或叫做 URI,是用来标识互联网上的资源(例如,网页或文件)和怎样访问这些资源的传输协议(例如,HTTP 或 FTP)的字符串.除了e ...
- 结合实例详细介绍encodeURI()、encodeURIComponent()、decodeURI()、decodeURIComponent()使用方法
在介绍encodeURI().encodeURIComponent().decodeURI().decodeURIComponent()方法前我们需要了解Global对象的概念: Global(全 ...
- javascript中escape()、unescape()、encodeURI()、encodeURIComponent()、decodeURI()、decodeURIComponent()比较
这些URI方法encodeURI.encodeURIComponent().decodeURI().decodeURIComponent()代替了BOM的escape()和unescape()方法.U ...
- decodeURI、decodeURIComponent 编码方法
——摘自<JavaScript高级程序设计> 编码: Global 对象的 encodeURI()和 encodeURIComponent()方法可以对 URI(Uniform Resou ...
- JS中encodeURI、encodeURIComponent、decodeURI、decodeURIComponent
js 对文字进行编码涉及2个函数:encodeURI,encodeURIComponent,相应2个解码函数:decodeURI,decodeURIComponent 1.用来编码和解码URI的 统一 ...
- JavaScript decodeURI()与decodeURIComponent() 使用与区别
decodeURI()定义和用法:decodeURI()函数可对encodeURI()函数编码过的URI进行解码.语法:decodeURI(URIstring)参数描述:URIstring必需,一个字 ...
随机推荐
- Linux下实现web服务器
说明:暂时只是实现了静态网页的响应 #include <stdio.h> #include <sys/types.h> /* See NOTES */ #include < ...
- Scala Map与Tuple
创建Map // 创建一个不可变的Map val ages = Map("Leo" -> 30, "Jen" -> 25, "Jack&q ...
- windows主机上ORACLE生成awr报告的步骤
oracle数据库是一个大型的关系型数据库,那么如果有一天装载数据库的主机由于大量的IO操作导致主机cpu荷载超过100%会使得主机卡顿或者对数据库连接或者进行数据库进行正常的IO操作都会产生影响,所 ...
- Java连接Jira,创建、修改、删除工单信息
还不了解Jira是什么的同学可以看一下这篇文章:https://www.cnblogs.com/wgblog-code/p/11750767.html 本篇文章主要介绍如何使用Java操作Jira,包 ...
- js中__proto__和prototype的区别和关系?(转)
转自知乎:https://www.zhihu.com/question/34183746
- Angular 学习笔记 (Material table sticky 原理)
更新 : 2019-12-03 今天踩坑了, sticky 了解不够深 refer http://www.ruanyifeng.com/blog/2019/11/css-position.html 阮 ...
- SQL Server邮件标识点
<br>---换行  :---空格 <H1></H1>---标题 --定义表格格式 N'<table border="1" ...
- IServiceBehavior IContractBehavior IEndpointBehavior IOperationBehavior
using System; using System.Collections.ObjectModel; using System.Reflection; using System.ServiceMod ...
- 记https在Android浏览器无法访问
问题描述 M站静态资源单独配置的https域名,在Android原生浏览器里面打开之后提示证书不安全,在chrome.UC之类的浏览器之下,静态资源都能够正常访问 问题原因 CA证书链不完整 http ...
- 多个div并排不换行
1.所有div的父元素不换行 white-space: nowrap; 2.所有div设置为行内元素 display: inline-block; 基于java记账管理系统[尚学堂·百战程序员]