0

I have a servlet that I want to send javascript via ajax call:

private String returnImage(String img, String imgFailOver) {
String str="<img src='http://Foo.com/images/"+img+".jpg' onError='this.onerror = null; this.src=\""+imgFailOver+".jpg\";' height='500' width='500'/><br/>";
return str;
}

Basically trying to have servlet send this:

<img src="image.png" onError="this.onerror=null;this.src='/images/noimage.gif';" />

From this SO doc.

My question/ problem is why do the escaped double quotes in servlet not register (get ignored) on the webpage? enter image description here

PS- I'm tagging Java and Javascript b/c I'm not sure what side to fix this on?

Community
  • 1
  • 1
Chris
  • 18,075
  • 15
  • 59
  • 77

1 Answers1

1

You need to put &quot; instead of \". The HTML parser in the browser is unable to read \", this is a Java double quote escape.

dcernahoschi
  • 14,968
  • 5
  • 37
  • 59