import image in javascript code example
Example 1: inject image javascript
button.innerHTML = "<img src="image/image.png">"
Example 2: how to set diferent images in html through js
<div id="x"></div>
<script>
var img = document.createElement("img");
img.src = "image.png";
var src = document.getElementById("x");
src.appendChild(img);
</script>
Example 3: javascript load image programmatically
var elements = [];
function loaded(){
console.log("you have loaded an image");
}
CreateFileFrom("your/dir/here/img.png");
function CreateFileFrom(dir){
/* defining runtime variables */
var extension = dir.split('.').pop();
var keys = {"png":"IMG","jpg":"IMG","jpeg":"IMG",
"js":"SCRIPT","json":"SCRIPT",
"mp3":"AUDIO","wav":"AUDIO"};
var obj = document.createElement(keys[extension]) || {};
obj.src = dir;
/* onload function called when the resource is loaded */
obj.onload = (e) => {
elements.push(e.path[0]);
loaded()
}
/* make sure that the data is compitable */
if(keys[extension]==null){console.error("not supported media type "+extension);return;}
}