Recently, I added a cool JavaScript code to my website that changes the background of the landing page randomly. Here is the snippet:
var bgImages = [
"url('assets/bg/front1.jpg')",
"url('assets/bg/front2.jpg')",
];
var randomIndex = Math.floor(Math.random() * 2) + 0;
document.getElementById("front").style.backgroundImage = bgImages[randomIndex];
Upon loading the page, I noticed an error in the console:
background.js:6 Uncaught TypeError: Cannot read property 'style' of null
at background.js:6
Strangely, when I manually run this script through the console, it works smoothly. Any suggestions or solutions would be greatly appreciated. Thank you in advance!