简体   繁体   中英

Set <img> as Base64 string Javascript

I am trying to set an image to a base64 encoded string in javascript right now I have this in my html

<img id="image_holder" src="" />

And then this in my javascript

document.getElementById("image_holder").src = "data:image/png;base64,iVBOR...";

And then I just see this in my webpage

在此处输入图片说明

Instead of the image it should be.

Thanks for the help.

Try using window.onload event

 window.onload = function() { document.getElementById("image_holder").src = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABUAAAAWCAYAAAAvg9c4AAAABHNCSVQICAgIfAhkiAAAACRJREFUOI1j/M/A8J+ByoCJ2gaOGjpq6Kiho4aOGjpq6GA2FAAxTAIqi7SS4AAAAABJRU5ErkJggg=="; } 
 <body> <img id="image_holder" /> </body> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM