In this code why this console error is coming? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

In this code why this console error is coming?

I am trying to make a preloader.. this code is just for practice but this is not working.. i don't know how to fix it.. .. https://code.sololearn.com/WL9gpMkEPR3i/?ref=app

11th Jun 2020, 6:31 AM
payal shah
payal shah - avatar
2 Answers
+ 3
Js is getting loaded before html ,so script tries to access a element which isn't available yet so you see that error ,to prevent js from getting loaded before html document Add your script in this function window.onload=function(){ } or add your script in html before closing body tag
11th Jun 2020, 6:44 AM
Abhay
Abhay - avatar
+ 1
document.getElementById() returns null if the element with specified id is not found. On sololearn JS is executed before HTML is loaded. Due to this you get a null instead of reference to HTMLElement. You can simple move first statement within load event listener. window.addEventListener("load",function(){ var preloader=document.getElementById("loading_bg"); preloader.style.display='none'; }); Also read: https://www.sololearn.com/post/90825/?ref=app As your page has not enough content it'll load immediately and you can't even see the #loading_bg. You can add an image that will take some time to load and till then #loading_bg will be visible.(If the image gets cached then it'll always load immediately on later visits to page and #loading_bg won't be visible) https://code.sololearn.com/W12yhsBxpn7V/?ref=app another example: https://code.sololearn.com/W8wTPD8Zpnkh/?ref=app
11th Jun 2020, 6:59 AM
🇮🇳Omkar🕉
🇮🇳Omkar🕉 - avatar