tags:

views:

16

answers:

2

in jsp i have

<a href="javascript:toggle('<s:property value="company.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?

A: 
  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">
    
Pointy
thanks for the example.
Ryan
A: 

I think it's worth looking at this SO question: http://stackoverflow.com/questions/1019618/escape-apostrophe-as-with-cout-jsp

naivists
I agree - that's the same issue; however I don't personally think that a tag is the best answer. I always use an EL function. To each his own of course.
Pointy
thanks for the link.
Ryan