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

这些方法用来对URL进行编码和界面,有什么区别?见下面列子:

原始URL

     var url = "http://dotnetcms.org/Hello启明星"

 (1)使用escape编码(escape编码已经过时了,不再推荐使用):

escape(url)
输出
http%3A//dotnetcms.org/Hello%u542F%u660E%u661F

(2)使用encodeURI

使用 encodeURI(url)
输出
http://dotnetcms.org/Hello%E5%90%AF%E6%98%8E%E6%98%9F

 (3)使用encodeURIComponent 编码

使用 encodeURIComponent(url) 
输出
http%3A%2F%2Fdotnetcms.org%2FHello%E5%90%AF%E6%98%8E%E6%98%9F

 从上面,可以很容易看到 encodeURI和encodeURIComponent 的区别:encodeURI会保留 http://dotnetcms.org 格式,而 encodeURIComponent 则会把 :// 也给编码。

 

正常情况下,我们使用ASP.NET的 Server.UrlDecode(url) 能进行解码:

在上面三种编码情况下,使用ASP.NET解码,

 string url1 = "http%3A//dotnetcms.org/Hello%u542F%u660E%u661F";
            string url2 = "http://dotnetcms.org/Hello%E5%90%AF%E6%98%8E%E6%98%9F";
            string url3 = "http%3A%2F%2Fdotnetcms.org%2FHello%E5%90%AF%E6%98%8E%E6%98%9F";

            Response.Write(Server.UrlDecode(url1));
            Response.Write("<br>");
            Response.Write(Server.UrlDecode(url2));
            Response.Write("<br>");
            Response.Write(Server.UrlDecode(url3));

 

下面是结果:

http://dotnetcms.org/Hello启明星
http://dotnetcms.org/Hello启明星
http://dotnetcms.org/Hello启明星

 如果不能返回结果,你可以使用  HttpUtility.UrlDecode(url,Encoding) 方法,第二个参数制定编码格式

转载于:https://www.cnblogs.com/mqingqing123/p/10708755.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值