A_Elric A_Elric - 1 year ago 129
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.

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download