简体   繁体   中英

Escape ampersand sign in javascript in ASP.NET

I have asp page which retrieves javascript code from code behind file. The code consists of companyName which sometimes have '&' sign. This javascript codeis used during onclick event of a link in the asp page. However it looks like whenever there is a comapnyName with '&' sign, it cuts of the string value after the '&' sign.

Code Behind:
    <sc:Link runat="server" Field="EmployeeLink" Item='<%# Eval("Item") %>' onclick='<%# Eval("OnClickJs") %>'>

ASP file:
    OnClickJS =  "var d=d_gi('" + Sitecore.Context.Item["ID"] + "');d.tl(this,'d','Page " + "|" +companyName +");";

When companyName is 'John Doe' => the link has 'John Doe' in link url

When companyName is 'John & Doe' => the link only has 'John' in link url

I tried escaping ampersand sign using '\\', '&' 'amp;' so far.

Any suggestions? Thanks.

Sanjeev

Use HttpServerUtility.UrlEncode for this:

http://msdn.microsoft.com/es-es/library/zttxte6w(v=vs.110).aspx

OnClickJS =  HttpServerUtility.UrlEncode("var d=d_gi('" + Sitecore.Context.Item["ID"] + "');d.tl(this,'d','Page " + "|" +companyName +")");

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