开发者

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?


  1. Don't use "href" for Javascript. It's icky. Instead, do this:

    <a href='#' onclick="toggle('whatever'); return false">
    
  2. 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 that fn:escapeXml() is not appropriate for use in Javascript source. I'm not sure what your s: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">
    
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜