Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis+ 1
How to add images (background) in JavaScript?
7 Respostas
+ 11
window.onload = () => {
document.body.style.backgroundImage = "url('https://i.picsum.photos/id/774/536/354.jpg')";
Or
const body = document.querySelector('body');
body.style.backgroundImage = "url('https://i.picsum.photos/id/774/536/354.jpg')"
};
+ 6
Yes, Sick LĢ
Ģ²iĢ
Ģ²nĢ
Ģ²eĢ
Ģ² BĢ¶rĢ¶oĢ¶š”ļøbro. I agree with you. šš
It took me some time to research to know the use of a single quote with URL.
https://stackoverflow.com/q/35402598/11325172
I found this thread on StackOverflow, similar to my query. :)
+ 5
window.onload = () =>{
let elem = document.body;
elem.style["background-image"] =
"url(https://picsum.photos/536/354)";
}
+ 5
"... that is totally not required. Even that double quote or single quote causes issue with some Version of IE š of course i know no one care about that."
Thanks Sick LĢ
Ģ²iĢ
Ģ²nĢ
Ģ²eĢ
Ģ² BĢ¶rĢ¶oĢ¶š”ļø bro for letting me know about that issue. š
"The format of a URL value is 'url(' followed by optional white space followed by an optional single quote (') or double quote (") character followed by the URL itself..."
Also, According to W3C standard it's optional. :P
https://www.w3.org/TR/CSS2/syndata.html#value-def-uri
+ 3
Taraash Mittal use this link:
Very useful!! https://www.w3schools.com/jsref/prop_style_backgroundimage.asp
+ 2
Thanks everyone guysšš
0
You could probably do it in html but in javascript I have no idea