URI:  Uniform Resource Identifier

encodeURI() And decodeURI()

  • The encodeURI() function is used to encode a URI.
  • The encodeURI() function encodes special characters, except: , / ? : @ & = + $ # (Use encodeURIComponent() to encode these characters).
  • The decodeURI() function is used to decode a URI.

Example

 <script>
var uri = "my test.asp?name=ståle&car=saab";
var enc = encodeURI(uri);
var dec = decodeURI(enc);
var res = "Encoded URI: " + enc + "<br>" + "Decoded URI: " + dec;
document.write(res);
</script>
// Result
Encoded URI: my%20test.asp?name=st%C3%A5le&car=saab
Decoded URI: my test.asp?name=ståle&car=saab

encodeURIComponent() And decodeURIComponent()

  • The encodeURIComponent() function encodes a URI component.
  • This function encodes special characters. In addition, it encodes the following characters: , / ? : @ & = + $ #.
  • The decodeURIComponent() function decodes a URI component.

Example

 <script>
var uri = "http://w3schools.com/my test.asp?name=ståle&car=saab";
var uri_enc = encodeURIComponent(uri);
var uri_dec = decodeURIComponent(uri_enc);
var res = "Encoded URI: " + uri_enc + "<br>" + "Decoded URI: " + uri_dec;
document.write(res);
</script>
// Result
Encoded URI: http%3A%2F%2Fw3schools.com%2Fmy%20test.asp%3Fname%3Dst%C3%A5le%26car%3Dsaab
Decoded URI: http://w3schools.com/my test.asp?name=ståle&car=saab

The different between encodeURI() and encodeURIComponent()

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.

param1 = encodeURIComponent("http://xyz.com/?a=12&b=55")

Then you may create the URL you need:

url = "http://domain.com/?param1=" + param1 + "&param2=99";

And you will get this complete URL:

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

TOOL: text converter

JavaScript encodeURI(), decodeURI(), encodeURIComponent(), decodeURIComponent()的更多相关文章

  1. javascript encodeURI和encodeURIComponent的比较

    总结:encodeURI对除三种字符()之外的字符进行编码 encodeURIComponent对除两种字符之外的字符进行编码,保留字符会被转义 在进行SaaS前端开发的时候,大家经常会用到两个Jav ...

  2. javascript - encodeURI和encodeURIComponent的区别

    这两个函数功能上面比较接近,但是有一些区别. encodeURI:不会进行编码的字符有82个 :!,#,$,&,',(,),*,+,,,-,.,/,:,;,=,?,@,_,~,0-9,a-z, ...

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

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

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

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

  5. JS中encodeURI、encodeURIComponent、decodeURI、decodeURIComponent

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

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

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

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

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

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

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

  9. 关于JavaScript中的escape、encodeURI和encodeURIComponent

    此文内容与关于JavaScript中的编码和解码函数 关联 escape() 方法: 采用ISO Latin字符集对指定的字符串进行编码.所有的空格符.标点符号.特殊字符以及其他非ASCII字符都将被 ...

随机推荐

  1. [LintCode] Reverse Integer 翻转整数

    Reverse digits of an integer. Returns 0 when the reversed integer overflows (signed 32-bit integer). ...

  2. 卸载VS2012

  3. 类库,委托,is和as运算符,泛型集合

    类库:其实就是一堆类文件,只不过用户看不到这些类的源代码,保密性好. 优点:保密性好缺点:如果这个方法不好用,使用者无法自己去更改它. 类文件是.cs    类库是.dll 新建项目为类库,在debu ...

  4. .net下Ueditor配置(主要讲解上传功能配置)

    Ueditor上传功能配置——.net 今天项目中用到Ueditor就自己配置了下,但上传功能和图片显示功能不能正确使用,就自己琢磨了下.已实现上传功能,并能显示正确的图片和文件,在线编辑功能也能使用 ...

  5. javascript事件有哪些?javascript的监听事件

    事件类型: 1.界面事件 onload:描述文档,图片,css已经frame,object加载完毕时触发,window.onload window.onload = function(){ //代表图 ...

  6. android之消息机制(二)

    消息通道:Looper 首先编写main.xml文件 代码如下: <LinearLayout xmlns:android="http://schemas.android.com/apk ...

  7. ARM的一些基本概念

    MPU介绍: mpu是一个芯片,重力加速器(加速度)和陀螺仪(角速度) iic总线.在板上有iic控制器 连接着 最多128个外设,每个外设有地址,可以通信. 寄存器: cpu中的寄存器是为了加快运算 ...

  8. Color Space: Lab

    Lab颜色空间是由CIE(国际照明委员会)制定的一种色彩模式.自然界中任何一点色都可以在Lab空间中表达出来,它的色彩空间比RGB空间还要大.另 外,这种模式是以数字化方式来描述人的视觉感应, 与设备 ...

  9. setAlpha方法 设置透明度

    public void setAlpha (int x) 其中,参数x为透明度,取值范围为0~255,数值越小越透明.

  10. 没有对“C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files”的写访问权限 的解决方案

    问题情况: 在64位机器上运行Web服务,然后在配置好之后测试访问的时候出现如下提示: