Hi. Both codes are the same but one runs and the other doesn't. Why is that? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0
4th May 2020, 11:31 PM
Addo Frank Ofosu
Addo Frank Ofosu - avatar
7 Answers
+ 1
Because he/she is probably using the browser to execute the code. Normally, doing as he/she, you should write the js code in a different file from the html and then link then using <script src = "/path to the file.js"> tag at the end of the body section in html. Doing so the js code will be executed after the html body has been interpreted by the browser engine. If you use the SoloLearn interpreter it will link the js and html parts by default but js will be interpreted first...that happens just here in SoloLearn not with browser engines.
5th May 2020, 12:30 AM
Alessandro Palazzolo
Alessandro Palazzolo - avatar
+ 1
You're welcome...ask if you have any doubt. I suggest you, if you are not already doing it, to practice within the browser, that's were you will mainly execute js/html/css code if you continue on this path.
5th May 2020, 12:48 AM
Alessandro Palazzolo
Alessandro Palazzolo - avatar
+ 1
It would be better with a computer...if you only have a phone then it's better if you continue with sololearn, but to handle more complicate codes you'll need a computer.
5th May 2020, 10:05 AM
Alessandro Palazzolo
Alessandro Palazzolo - avatar
0
You have to make the wole js code be a function triggered by the onload event of the window. Otherwise the js code will be executed before the html and the "document.getElementById()" will return a "null" value. Hope that to be useful, ask if you have any doubt.
5th May 2020, 12:15 AM
Alessandro Palazzolo
Alessandro Palazzolo - avatar
0
Yes I know that but from the video I am learning from he or she didn't use an onload for the entire code.
5th May 2020, 12:23 AM
Addo Frank Ofosu
Addo Frank Ofosu - avatar
0
Oh okay Thanks
5th May 2020, 12:37 AM
Addo Frank Ofosu
Addo Frank Ofosu - avatar
0
Can you do that on your phone(i mean within the browser) . I am using notepad++ on my phone but it keeps on crashing
5th May 2020, 1:02 AM
Addo Frank Ofosu
Addo Frank Ofosu - avatar