+ 1
How to add images (background) in JavaScript?
9 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