+ 1
How can I print image to the screen using js?
var john = { name: "John", age: 25 }; var perfil = ["https://i.imgur.com/i8rIUL7.jpg"] document.write(john.name) document.write(john.age) document. // your code goes here
5 Answers
+ 5
document.write can't print an image to the web page
if you want to print an image , you can do it with console.log()
here is the example of printing an image inside the console
console.log('%c ', 'font-size:400px; background:url(https://i.imgur.com/i8rIUL7.jpg) no-repeat;');
but if you want to show an image in a webpage you can use DOM
this is the code example:
const show_image=(src, width, height, alt) => {
var img = document.createElement("img");
img.src = https://i.imgur.com/i8rIUL7.jpg;
img.width = width;
img.height = height;
img.alt = alt;
// This next line will just add it to the <body> tag
document.body.appendChild(img);
}
+ 5
NimaPoshtiban,
I think you meant to use <src> parameter
img.src = src;
Inside show_image() function?
+ 2
Yeah
+ 2
Thank you for yours support
+ 1
you could even document.write() image, if only you format it as html string:
document.write('<img src="https://i.imgur.com/i8rIUL7.jpg">');
however, document.write will open a new document as soon as the page was completly parsed (loaded), so you could reset the content of the page by using it ^^
it's more advised to use 'innerHTML' property of a targeted selected element to assign to it the html string ;P
anyway, the best practice is to append/insert to the page a new image element created through js, as suggested by NimaPoshtiban...