Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialScott Nichols
3,331 PointsHow would I dynamically create constants in javascript between one or more javascript files
I have created a Sketchup Ruby interface program in which I incorporate custom UI features via HTML and javascripts. I'm needing to write a set of global constants or variables to a file that defines the directory sets for this utility. Once I define the directory set environment from Sketchup Ruby, I need to export the constants/variables so that the javascript file(s) associated with one or more HTML documents can access the other files. For instance, my development environment will be different than than an installed application environment. In (Sketchup) Ruby files, MyUIToolsRootDir = D:/myDevelopmentDir/projectsDir/MyUIToolsDir. When installed the MyUIToolsRootDir = C:/{Sketchup Plugins Dir}. I need to output to a file that my javascripts can access and use the constants or variables that define these different Environments using the same variable names, prior to opening the HTML and javacript files. Is there an include statement that would automatically link these values that can be used across one or more javascript files? Thanks, Scott.
1 Answer
Steven Parker
231,269 PointsWhen you say "prior to opening the HTML and javacript files", I wonder if you actually need dynamic loading. If the files are created in advance, then the HTML can simply contain <script>
tags that reference the common files.
But if you do need this to happen dynamically within an already running script, scripts that have a type of "module" can use the import statement to load other code. For more details, see the MDN page for import.
Also, I found this article that discusses techniques for doing it without using "import" through DOM manipulation: JavaScript Madness: Dynamic Script Loading.