+ 5
Why do I have this error?
It always happens to me and I do not understand why :( https://code.sololearn.com/WReSV7m3sdaA/?ref=app
2 Réponses
+ 3
It's because the JS is getting loaded before the webpage itself.
You need to wrap the entire JS code inside a function which fires after the window has been loaded.
Do this,
window.onload= function(){
//Your JS goes here
}
+ 1
Look the body tag: <body onload="func()">
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body onload="func()">
<div id="one"></div>
</body>
</html>
JS:
function func(){
var one = document.querySelector("#one");
one.innerHTML = "one";
}