A_Elric A_Elric - 1 month ago 4x
Java Question

Escaping javascript string in java

I need to make this into a string in java:

<script type="text/javascript">document.write("<img src=\"UpArrow.png\" /> \"); </script>

Can someone help? I keep trying and it ends up like this...

return "<script type=\"text/javascript\">document.write(\"<img src=\"UpArrow.png\" /> \"); </script>";

Which doesn't work because I need to double escape the quotes before and after UpArrow.png. since it needs to be escaped in javascript and not in java.


Apache commons have a methods just for this in StringEscapeUtils : the escapeJavaScript method.