+ 1
My javascript animation does not work
https://code.sololearn.com/WH1o75rP9t2d When you click the eyes of the robot or the left arm, they are supposed to move but they don't. I also get an error message: JavaScript Console TypeError: rightEye is null Line: 81 Line 81 doesn't even exist. Is it a bug in the sololearn interface? It does work in jsfiddle though and the code is exactly the same (copy-paste) https://jsfiddle.net/watzthis/6s0atb88/
1 Answer
+ 9
The js ( and css ) tab is virtually and silently linked to the html one, as it was linked ( or embed ) in the <head> part of the html code... so, your js code need to wait for full load of the page content before accessing the DOM ^^
To do that, just put your code ( or only your initialization code ) in a function called on 'load' event of window object. With an anonymized fonction:
window.onload = function() {
/* your code */
};
... or, more 'clean', with the dedicated method ( example with a named function this time, but both are possible in all cases ):
function main() {
/* your code */
}
window.addEventListener('load', main);
Anyway, you can run immediatly script parts that doesn't require to access DOM ;)