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...
1
  • No way of knowing without seeing more code, such as how mySrc is defined and populated. Commented Dec 21, 2011 at 19:29

1 Answer 1

2

You should set src without apostrophes:

document.getElementById(id).src=mySrc;
Sign up to request clarification or add additional context in comments.

4 Comments

But there is no host text at all; I checked :(
what exactly contain mySrc? Where is the declaration ?
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 127.0.0.1:8080/%27 and that makes my image "invisible" :( So I think where the 127.0.0.1:8080/%27 comes from because it doesn't allow to show image right?
Thank to your notice I took off apostrophe(s) and all worked fine :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.