title: Escaping javascript string in java
body: <p>I need to make this into a string in java:</p>
  
  <pre><code> &lt;script type="text/javascript"&gt;document.write("&lt;img src=\"UpArrow.png\" /&gt; \"); &lt;/script&gt;
  </code></pre>
  
  <p>Can someone help?  I keep trying and it ends up like this...</p>
  
  <pre><code>return "&lt;script type=\"text/javascript\"&gt;document.write(\"&lt;img src=\"UpArrow.png\" /&gt; \"); &lt;/script&gt;";
  </code></pre>
  
  <p>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.</p>
  
  <p>.</p>
  
  <p>.</p>
  
  <p><strong>2019 Update:</strong> If you are looking at this, god help your soul.  This is awful code and if you're trying to do things this way you're doing it wrong (As others suggested to me).</p>
  
  <p>The correct way to do this would be jquery or one of the zillion DOM-modifying frameworks that exist now and popping stuff into / out of the scope of the DOM.  </p>
  
  <p>If you are doing this, you should not look at the code above or the solutions below, but should instead go learn more, as this is a path to make spaghetti code.</p>