To avoid a string being a Javascript string literal, you replace the backslash with double backslashes and a backslash and delimiter string separator:
<a onclick="AddressHandler.ProcessAddress('<%= homeAddress.Replace(@"\", @"\\").Replace("'", @"\'") %>');" class="button-link">change</a>
Note. javascript: protocol javascript: used when you put a script in a url and not as an event handler.
Edit:
If the script also contains characters that require HTML encoding, this should be done after escaping the Javascript string:
<a onclick="<%= Html.Encode("AddressHandler.ProcessAddress('" + homeAddress.Replace(@"\", @"\\").Replace("'", @"\'") +"');") %>" class="button-link">change</a>
So, if you donβt know what the string contains in order to be safe, you need to first avoid the string literal and then HTML code the code so that it can be placed in the attribute of the HTML tag.
Guffa source share