0

I am using quite a bit of javascript in which it would be rather helpful if I could use global constants and a few methods that I use all over my web site.

Instead of having to copy and paste these into each js file, is there a way to include them?

like:

#include global.js

jsCode

2 Answers 2

3

There are multiple techniques for module architecture

  • include multiple script tags and use namespaces to avoid global scope issues
  • use asynchronous file loaders like requireJS
  • use module packagers like modul8

The latter techniques are probably overkill, you probally want a

<script src="global.js" />

in your html master page.

Sign up to request clarification or add additional context in comments.

1 Comment

Hi, ok so if i include it in the html page, then i can reference a function inside the global.js from within another js file as long as it is included after the global.js?
1

Unfortunately, there's no standard JavaScript way to include one JavaScript file into another JavaScript file (I believe this is one of those lovely JavaScript's peculiarities.)

But, before you start to choose work-around, note that it's often considered a good practice to merge JavaScript files all over the project into a single big file (in short, this technique reduces number of HTTP requests and improves performance, for more explanations and caveats see Merging multiple javascript files.)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.