+ 1

Global objects in JavaScript

I have a HTML file and some js files. I created an objetc in one of these js files. The object is accessible from the HTMl file, but not from other js files. How can I make an object global?

5th Feb 2018, 8:23 PM
Manuel 48
Manuel 48 - avatar
3 Answers
+ 7
What do you mean? Can you show your code? try removing var, let, const which are used to declare local variables instead of global. Also, make sure other scripts have loaded first before execution.
5th Feb 2018, 8:44 PM
Valen.H. ~
Valen.H. ~ - avatar
+ 5
If you remove var, the variable you create will be global (on page) and won't only exist inside the imported script itself. This way scripts will be able to access vars of other scripts.
6th Feb 2018, 12:41 PM
Valen.H. ~
Valen.H. ~ - avatar
0
Ok, the script is loaded first in the html document and it starts immediately. And what do you mean with remove var? Isn't it a wrong js command?
6th Feb 2018, 12:38 PM
Manuel 48
Manuel 48 - avatar