+ 1

Why this code is showing getContext () is error

It is showing cannot read getContext () of null . https://code.sololearn.com/W6mrXACbXQi9/?ref=app

18th Aug 2020, 11:42 AM
🎶💞Sravs💞🥀
🎶💞Sravs💞🥀 - avatar
6 Answers
+ 3
https://www.sololearn.com/post/90825/?ref=app
18th Aug 2020, 11:58 AM
🇮🇳Omkar🕉
🇮🇳Omkar🕉 - avatar
+ 3
On sololearn, code written in javascript tab runs before the code in HTML tab. As a result JS tries to access HTML DOM before it exists. As DOM is not ready `document.querySelctor` returns `null`. You can try this solution, Wrap code within a callback function to `load` event as below : window.addEventListener('load',function(){ //code here. }); Or, assign a function to `window.onload` property: window.onload=function(){ //code here }; There is also third way. Write in script tag below body tag . . . </body> <script> //code here </script> . . . But I'll suggest don't use this last method.You'll not get code highlighting. First method is best.
18th Aug 2020, 12:54 PM
🇮🇳Omkar🕉
🇮🇳Omkar🕉 - avatar
+ 2
🎶💞Sravs💞🥀 , Have you read the post that I linked in previous answer?
18th Aug 2020, 12:25 PM
🇮🇳Omkar🕉
🇮🇳Omkar🕉 - avatar
+ 1
Could you explain clearly, where I have did mistake..
18th Aug 2020, 12:05 PM
🎶💞Sravs💞🥀
🎶💞Sravs💞🥀 - avatar
0
Yes,but I am confusing
18th Aug 2020, 12:36 PM
🎶💞Sravs💞🥀
🎶💞Sravs💞🥀 - avatar
0
Tq
19th Aug 2020, 2:14 AM
🎶💞Sravs💞🥀
🎶💞Sravs💞🥀 - avatar