2

I'm trying to set <img> src with JavaScript, but the image is missing visually; it is empty. The image URL after running this code starts with "http://127.0.0.1:8080/%27" (I don't even know where this came from) instead of "data: image/png". The variable mySrc is set to a Base64-encoded image.

document.getElementById(id).src="'"+mySrc+"'";

This is what the URL looks like after setting it:

http://127.0.0.1:8080/%27data:image/png;base64,/9j/4AAQSkZJRgABAQEAYABgAAD/4RMNRXhpZgAASUkqAAgAAAAQAAABAwABAAAAuAsAAAEBAwABAAAA9gkAAAIBAwADAAAAzgAAAAMBAwABAAAAAQAAAAYBAwABAAAAAgAAAA4BAgAfAAAA1AAAABIBAwABAAAAAAAAABUBAwABAAAAAwAAABoBBQABAAAA9AAAABsBBQABAAAA/AAAABwBAwABAAAAAQAAACgBAwABAAAAAgAAADEBAgALAAAABAEAADIBAgAUAAAAEAEAADsBAgAEAAAAUm9uAGmHBAABAAAAJAEAAHwBAAAIAAgACABJbWFnZSBjb252ZXJ0ZWQgdXNpbmcgaWZmdG9hbnkAAMDGLQAQJwAAwMYtABAnAABQaWNhc2EgMy4wAAAyMDA4OjA2OjA2IDEwOjM2OjEyAAQAAaADAAEAAAD...
ThinkingStiff
  • 64,767
  • 30
  • 146
  • 239
user592704
  • 3,674
  • 11
  • 70
  • 107
  • No way of knowing without seeing more code, such as how `mySrc` is defined and populated. – Chad Dec 21 '11 at 19:29

1 Answers1

2

You should set src without apostrophes:

document.getElementById(id).src=mySrc;
ThinkingStiff
  • 64,767
  • 30
  • 146
  • 239
abuduba
  • 4,986
  • 7
  • 26
  • 43
  • But there is no host text at all; I checked :( – user592704 Dec 21 '11 at 19:40
  • what exactly contain `mySrc`? Where is the declaration ? – abuduba Dec 21 '11 at 19:55
  • It is an image which I converted to base64 and placed the base64 into mySrc var; It is showing fine but if I set src with JS, as I show it in my question, the image info starts with http://127.0.0.1:8080/%27 and that makes my image "invisible" :( So I think where the http://127.0.0.1:8080/%27 comes from because it doesn't allow to show image right? – user592704 Dec 21 '11 at 20:01
  • Thank to your notice I took off apostrophe(s) and all worked fine :) – user592704 Dec 22 '11 at 17:08