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

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

原始URLhtml

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

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

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 编码url

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

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

 

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

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

 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));

 

下面是结果:blog

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

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

相关文章
相关标签/搜索