Forum Moderators: open
In JavaScript/JSP, i have code like this:
arrWorkList[<%= i %>] = {
atributoA:"<%= i + 1 %>",
atributoB:"<%= (line[1]%>" }
Since i have no control on what the JSP will put on line[1], it can have (and *does* have) words like
"Café d'Ouro"
so i get a error either i have
atributoB:"<%= (line[1]%>"
atributoB:'<%= (line[1]%>'
so what alternative have i?
Thanks a lot.
<%= (line[1]).replace("'","''") %> I'm not totally sure on the syntax or whether those are the right quotes, but hopefully you get the idea.
replace method [java.sun.com]
However i wonder what can i do if i wanted a client-side Javascript only solution, as that is one situation that may occur.
Thanks again.
If you pass a variable to Javascript, you can do escaping very easily, so hopefully that's the situation in which you'll need it? But your Java code obviously can't pass Javascript a variable, so escaping these at the server is really the way to go.
When you said "escaping very easily" i thougth there was something like the jarkata commons StringEscapeUtilities, that realy does it simply...
So you say i must use replace to replace ' with '', " with \", & with & and the rest? That's not "very easily", i mean, it's easy but a lot of typing...
Oh well, so be it...
Thanks a lot.