简体   繁体   中英

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. I'm having a problem passing querystring variables to that popup window, since the url is built server side.

Example

JS:

function popupwindow(vLink) { 
    window.open(vLink, 'Detail','width=600px,height=545px,status=yes,help=no,scrollbars=yes,resizable=yes,top=350');
}

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:

http://detail.aspx?item=123&code=ABC

Instead, I get something like this: http://detail.aspx?item=123&amp%3bcode=ABC

How can I prevent this from happening?

您可以尝试使用JavaScriptSerializer进行编码:

lblOpener.Attributes.Add("onclick", "popupwindow(" & New JavaScriptSerializer().Serialize(vLink) & ");");

Will encodeURI work? Wrap your vLink like so:

lblOpener.Attributes.Add("onclick", "popupwindow('" & encodeURI(vLink) & "');");

Might work!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM