handling strings with apostrophes passed to javascript generated by jsp and struts 2
in jsp i have
<a href="javascript:toggle('<s:property value="c开发者_StackOverflow中文版ompany.name"/>')">
company names containing apostrophes are generated like so:
<a href="javascript:toggle('apostrophe's company')"/>
single quotes around names cause problems for company names with the apostrophes. i'd like to get double quotes around the names instead, but i've tried a bunch of things and not having much success. any suggestions?
Don't use "href" for Javascript. It's icky. Instead, do this:
<a href='#' onclick="toggle('whatever'); return false">
To deal with quotes in Javascript, I've always written an Expression Language function to perform the Javascript equivalent of
${fn:escapeXml(whatever)}
. I have no idea why that's not part of the standard JSTL/EL function suite, but there you go. Note thatfn:escapeXml()
is not appropriate for use in Javascript source. I'm not sure what yours:property
thing is; it might be necessary for you to do something like this:<c:set var='companyName'><s:property value='company.name'/></c:set> <a href='#' onclick="toggle('${foo:escapeJS(companyName)}'); return false">
精彩评论