2011-03-30 66 views
1

ASP.NET 4.0,webform站点使用javascript窗口打开url编码

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

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

不过,我需要为做URL编码,所以它变成 “anotherURI参数1 = AAA & 26param2 = BBB?”:

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

现在,如果我要附上这与JavaScript,它不会工作。我如何再次对网址进行编码?

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

回答

4

更正URI:

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

RIGHT: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") 

如果你想从该变量解码网址:

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

然而, param1&param2用于另一个URI。这是如何运作的? – Stan 2011-03-30 20:53:35

+1

@fan.aspx'页面中的Stan读取'Request.QueryString [“anotherURI”]的值 - 它将包含像'param1 = aaa&26param2 = bbb'这样的原始值,因此您需要使用Split自己解析它。如果您需要进一步的帮助,弗拉维奥将无法使用,请告诉我,我会拉快示例。 – 2011-03-30 21:13:22

+0

对不起,如果我没有完成解释到这个级别,让我们举例说明: 从asp.net上的queryString变量获取编码值: – 2011-03-31 07:14:03

1

,如果你想编码/解码它(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')); 
}