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

这三个编码函数对 ASCII 字母和数字都不编码,但是对符号的编码有所不同。

<!DOCTYPE html>
<html>
<head>
<meta charset='UTF-8'>
<title></title>
</head>
<body>
<div id='container'>
</div> </body>
<script src="jquery.js"></script>
<script src="jquery.tmpl.js"></script>
<script type="text/x-jquery-tmpl" id="tmpl">
<div id='url'>url--------------------:${url}</div>
<div id='url1'>escape----------------:${url1}</div>
<div id='url2'>encodeURI-------------:${url2}</div>
<div id='url3'>encodeURIComponent----:${url3}</div>
</script>
<script type="text/javascript">
var url = 'url +/?%#&=';
var url1=escape(url);
var url2=encodeURI(url);
var url3=encodeURIComponent(url);
var params = {
url:url,
url1:url1,
url2:url2,
url3:url3
};
var tmpl = $('#tmpl').tmpl(params);
$('#container').append(tmpl);
</script>
</html>

输出:

url--------------------:url +/?%#&=
escape----------------:url%20+/%3F%25%23%26%3D
encodeURI-------------:url%20+/?%25#&=
encodeURIComponent----:url%20%2B%2F%3F%25%23%26%3D

所以我们使用encodeURIComponent对url编码

javascript之url转义escape()、encodeURI()和encodeURIComponent()的更多相关文章

  1. javascript之url转义escape()、encodeURI()和decodeURI()

    我们可以知道:escape()除了 ASCII 字母.数字和特定的符号外,对传进来的字符串全部进行转义编码,因此如果想对URL编码,最好不要使用此方法.而encodeURI() 用于编码整个URI,因 ...

  2. javascript之url转义escape()、encodeURI()和decodeURI(),ifram父子传参参数有中文时出现乱码

    ifram父子传参参数有中文时出现乱码,可先在父级页面用encodeURI转义,在到子页面用进行decodeURI()解码 我们可以知道:escape()除了 ASCII 字母.数字和特定的符号外,对 ...

  3. Javascript中escape(), encodeURI()和encodeURIComponent()之精析与比较

    escape(), encodeURI()和encodeURIComponent()是在Javascript中用于编码字符串的三个常用的方法,而他们之间的异同却困扰了很多的Javascript初学者, ...

  4. JS中 escape, encodeURI 和 encodeURIComponent的区别

    为避免Url字符串在传递过程中的乱码,我们一般需要对字符串进行处理. 在Javascript中实现此功能的全局对象有3个,分别是:escape(),  encodeURI()  和 encodeURI ...

  5. escape(), encodeURI()和encodeURIComponent()(转)

      escape(), encodeURI()和encodeURIComponent()是在Javascript中用于编码字符串的三个常用的方法,而他们之间的异同却困扰了很多的Javascript初学 ...

  6. escape encodeURI和encodeURIComponent的区别

    escape(与之对应->unescape) escape是对字符串(string)进行编码(而另外两种是对URL),作用是让它们在所有电脑上可读.编码之后的效果是%XX或者%uXXXX这种形式 ...

  7. javascript进行url转义方法比较escape、encodeURI和encodeURIComponent

    escape会将除了 ASCII 字母.数字和特定的符号(* @ - _ + . /)以外的字符全部进行转义编码,因此如果想对URL编码,最好不要使用此方法,因为它会让你的URL变的不可读. 提示:E ...

  8. JavaScript中编码函数escape,encodeURI,encodeURIComponent

    第一:escape():对字符串进行编码,escape()不编码的字符:@*/+ 第二:encodeURI() 函数可把字符串作为 URI 进行编码.不会进行转义的:;/?:@&=+$,# 第 ...

  9. url编码函数encodeURI和encodeURIComponent

    var url = "http://www.wrox.com/illegal value.html#start";encodeURIComponent(url)  //" ...

随机推荐

  1. 重构第18天 用条件语句来代替异常(Replace exception with conditional)

    理解:本文中的“使用条件判断代替异常”是指把没有必要使用异常做判断的条件尽量改为条件判断. 详解: 重构前代码: public class Microwave { private IMicrowave ...

  2. 适用于jquery1.11.1的ajaxfileupload.js

    ajaxfileupload源码 解决上传成功不走success的问题 解决高版本jquery兼容性问题 jQuery.extend({ createUploadIframe: function(id ...

  3. 如何理解css中的float

    最近一段时间一直在为一个即将上线的新站进行一些前端开发.自然,对CSS的使用是必不可少的了.我们在CSS 中很多时候会用到浮动来布局.常见的有 float:left 或者 float:right .简 ...

  4. EasyUI个人项目晒图(续)

    晒自己做的一个管理系统(清新风格)EasyUI 这是自己上一次的文章了,只是给大家看一下自己的美观度是不是还是停留在新手的阶段!反正我自己认为我已经不是一个新手了吧!虽然技术永远学不完,我可以说,我和 ...

  5. [moka同学笔记]bootstrap基础

    1.导航栏的制作 <nav class="nav navbar-default navbar-fixed-top" role="navigation"&g ...

  6. iOS UITableview

    1. UITableView //去除tableviews的点击效果 cell.selectionStyle = UITableViewCellSelectionStyleNone;    //隐藏t ...

  7. Natural language style method declaration and usages in programming languages

    More descriptive way to declare and use a method in programming languages At present, in most progra ...

  8. Ogre2.1 灯光与阴影

    Ogre2.1大量光源渲染 Ogre2.1不是采用现在大部分引擎所用的延迟渲染,而是采用一种前向渲染的改进技术,理论基本来自于Forward+,见如下. http://www.klayge.org/? ...

  9. 深入理解MySQL开发性能优化.pptx

    深入理解MySQL开发性能优化.pptx,依旧上传baidu pan http://pan.baidu.com/s/1jIwGslS,视频暂未出,培训完成后会更新.

  10. NYOJ 21 三个水杯

    三个水杯 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   描述 给出三个水杯,大小不一,并且只有最大的水杯的水是装满的,其余两个为空杯子.三个水杯之间相互倒水,并且水杯没有 ...