Jump to content

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


Recommended Posts


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" ,
        "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.


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

Thank-you for your help.


Link to comment
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>
This code uses the array


  • Like 1
Link to comment
Share on other sites


That worked!

That was easy!

Thank-you.  This question is "solved".


Link to comment
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

  • Create New...