Jump to content
W3_Bill

How to do something like C++ "include file"

Recommended Posts

Background:

In C++, I can put a group of definitions into a separate file, and then in the C++ source, do a "#include <include_file_path>" to bring those definitions into the C++ file at the desired location.  An example would be putting basic physics constants (gravitational constant, electron charge, dry gas constant, electron mass, speed of light in a vacuum, Plank's constant, etc.) into a file "physics_constants.H", and then a physics application program would have a line

#include "physics_constants.H"

In a JavaScript, I have an array of photo captions (text).  Here's a piece:

var captions = [
        "Longs Peak on March 17, 2016" + "<br />" +
             "Colorado, USA" ,
        "Mt. McKinley on January 19, 2015" + "<br />" +
             "Alaska, USA" ,
        [etc.]
        "Medicine Bow Peak on February 14, 2000" + <br/>" +
             "Wyoming, USA"
    ];

The array actually has 58 entries.  Rather than embedding that into the JavaScript (which is in turn a part of an html file), I'd like the array definition to be in a separate file by itself, and then put a JavaScript equivalent of the C++ "#include <include_file_path>" at the appropriate place in the JavaScript.

Questions:

Does JavaScript have such a capability?  If yes, how do I do it?

Thank-you for your help.

Bill.

Share this post


Link to post
Share on other sites

You can put a <script> tag with a src attribute preceding your code. Some people like to use Javascript to generate script tags, but if you can just write the HTML for it I find that to be a better solution.
 

<script src="arrays.js"></script>
<script>
This code uses the array
</script>

 

  • Like 1

Share this post


Link to post
Share on other sites

Wow!

That worked!

That was easy!

Thank-you.  This question is "solved".

Bill.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×