Local or Global? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Local or Global?

In JavaScript When we create a GetElementById array variable before a function it's considered as a global or just a local var? in fact I have this problem and I think it's linked to the DOM system rules .I juste can't call it in my function. Any ideas?

4th Oct 2017, 12:33 PM
Karim Boujnan
Karim Boujnan - avatar
2 Answers
+ 2
If you're out in the main script body, those are globals. I believe this also happens in functions if you don't use var. If you're creating the element in the JS tab, the JS tab loads into <head> before the DOM is ready. This includes any functions that run in <head> too early for the DOM. Use onload() -- Javascript is event-driven -- or call it at the end of <body>. This latter is reliable on SoloLearn only; see jQuery's "document ready" strategy for more info. Some people use setTimeout(). This leaves you open to a race condition and (however unlikely) probably shouldn't be done. If none of this applies could you post some code and the error you're receiving?
4th Oct 2017, 1:38 PM
Kirk Schafer
Kirk Schafer - avatar
+ 2
That's great Dude ! but in my case i found where was the problem and this is important to all of us! , cause we ignore that , the problem was in the HTML's javascript file call , it was like that <script src="Xray.js" type=" text/javascript" > </script> it look normal and it worked normal with me before. but this time it worked just when i added the charset value which is utf-8 and the async defer keywords .In fact the reason why is in the beginning of HTMl when you let the compiler to suggest the HTML syntax that's will adds all the values like http-equiv and content ....which force us to make a compatible script call . i wish i was able to explain the situation .thank you dude .
6th Oct 2017, 10:45 PM
Karim Boujnan
Karim Boujnan - avatar