• 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) 方法,第二个参数制定编码格式

  • 相关阅读:
    前端页面模拟浏览器搜索功能Ctrl+F实现
    正则表达式中?=和?:和?!的理解
    JRebel激活教程
    BAT脚本一键启动多个程序
    WinInet简介及操作流程
    通过线程传递消息
    两串口收发测试
    获取PC可用串口端口,并将其在combo box中显示
    为MFC应用程序添加登录对话框界面
    Using CInternetFile open an Url
  • 原文地址:https://www.cnblogs.com/mqingqing123/p/10708755.html
Copyright © 2020-2023  润新知