【问题标题】:url encoding with javascript window open打开 javascript 窗口的 url 编码
【发布时间】:2011-03-30 19:02:52
【问题描述】:

ASP.NET 4.0,网站表单

我的网站有一个链接是调用另一个页面并传递url参数,如下所示。

http://www.foo.com/foo.aspx?anotherURI?param1=aaa&26param2=bbb

但是,我需要对“anotherURI?param1=aaa&26param2=bbb”进行 url 编码,所以它变成:

http://www.foo.com/foo.aspx?anotherURI?param1%3Daaa%26param2%3Dbbb

现在,如果我想用 javascript 将其括起来,它就行不通了。如何再次对 url 进行编码?

javascript:void(window.open('http://www.foo.com/foo.aspx?anotherURI?param1%3Daaa%26param2%3Dbbb', 'popup'))

【问题讨论】:

    标签: javascript asp.net urlencode url-encoding


    【解决方案1】:

    更正 URI:

    错误:http://www.foo.com/foo.aspx?anotherURI?param1%3Daaa%26param2%3Dbbb

    右:http://www.foo.com/foo.aspx?anotherURI=param1%3Daaa%26param2%3Dbbb

    多个 URI 的示例:http://www.foo.com/foo.aspc?uri1=[encodedURI]&uri2=[encodedURI2]

    从 asp.net 上的 queryString 变量中获取值:

    Dim sUrl1 as String = request("VarName")
    Dim sUrl2 as String = request("VarName")
    Dim sUrl3 as String = request("VarName")
    

    如果您想从该变量中获取解码后的 URL:

    Dim sDecodedUrl1 as String = Server.UrlDecode(sUrl1)
    Dim sDecodedUrl2 as String = Server.UrlDecode(sUrl2)
    Dim sDecodedUrl3 as String = Server.UrlDecode(sUrl3)
    

    【讨论】:

    • 但是,param1 & param2 用于另一个 URI。它是如何工作的?
    • @Stan 在foo.aspx 页面中读取了Request.QueryString["anotherURI"] 的值——它将包含param1=aaa&26param2=bbb 之类的原始值,因此您需要使用Split 自己解析它。如果您需要进一步的帮助而 Flavio 将无法使用,请告诉我,我会快速举个例子。
    • 对不起,如果我没有完成这个级别的解释,让我举例说明:从 asp.net 上的 queryString 变量中获取编码值:
    • 是的,这行得通,您只需在将 url 设置为变量值之前对其进行编码...
    【解决方案2】:

    如果你想对其进行编码/解码(php 的方式)使用

    function url_encode(str) {
        str = (str + '').toString();
        return encodeURIComponent(str).replace(/!/g, '%21').replace(/'/g, '%27').replace(/\(/g, '%28'). replace(/\)/g, '%29').replace(/\*/g, '%2A').replace(/%20/g, '+');
    }
    
    function url_decode(str) {
        return decodeURIComponent((str + '').replace(/\+/g, '%20'));
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多