Maybe I'm just thinking about this too hard, but I'm having a problem figuring out what escaping to use on a string in some javascript inside a link's onClick handler. Example:
<a href="#" onclick="SelectSurveyItem('<%itemid%>', '<%itemname%>'); return false;">Select</a>
The <%itemid%>
and <%itemname%>
are where template substitution occurs. My problem is that the item name can contain any character, including single and double quotes. Currently, if it contains single quotes it breaks the javascript. My first thought was to use the template language's function to javascript-escape the item name, which just escapes the quotes. That will not fix the case of the string containing double quotes which breaks the HTML of the link. How is this problem normally addressed? Do I need to HTML-escape the entire onClick handler? If so, that would look really strange since the template language's escape function for that would also HTMLify the parentheses, quotes, and semicolons... Your input is much appreciated.
Edit: This link is being generated for every result in a search results page, so creating a separate method inside a javascript tag is not possible because I'd need to generate one per result.
Also, I'm using a templating engine that was home-grown at the company I work for, so toolkit-specific solutions will be of no use to me.