[英]Javascript window.open() escaping ampersands
I am upgrading a legacy application and it uses the annoying practice of using Javascript window.open()
to open popup windows in order to set values. 我正在升级旧版应用程序,它使用了烦人的做法,即使用Javascript window.open()
打开弹出窗口以设置值。 I'm having a problem passing querystring variables to that popup window, since the url is built server side. 我在将querystring变量传递到该弹出窗口时遇到问题,因为url是在服务器端构建的。
Example 例
JS: JS:
function popupwindow(vLink) {
window.open(vLink, 'Detail','width=600px,height=545px,status=yes,help=no,scrollbars=yes,resizable=yes,top=350');
}
XML XML
<asp:Label ID="lblOpener" runat="server" Text="_"></asp:Label>
Server Side 服务器端
int ditem = 123;
string dcode = "ABC";
string vLink = string.Format("detail.aspx?item={0}&code={1}", ditem, dcode);
lblOpener.Attributes.Add("onclick", "popupwindow('" & vLink & "');");
When the label is clicked I am expecting the popup to open with the querystring: 单击标签后,我希望弹出窗口以querystring打开:
http://detail.aspx?item=123&code=ABC
Instead, I get something like this: http://detail.aspx?item=123&%3bcode=ABC
相反,我得到的是这样的东西: http://detail.aspx?item=123&%3bcode=ABC
How can I prevent this from happening? 如何防止这种情况发生?
您可以尝试使用JavaScriptSerializer进行编码:
lblOpener.Attributes.Add("onclick", "popupwindow(" & New JavaScriptSerializer().Serialize(vLink) & ");");
Will encodeURI work? 可以使用encodeURI吗? Wrap your vLink
like so: 像这样包装您的vLink
:
lblOpener.Attributes.Add("onclick", "popupwindow('" & encodeURI(vLink) & "');");
Might work! 可能会工作!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.