0

I have seen many ways to include a JS file in another JS file, but all of them seem to require the files to be located on a server, as opposed to on the local drive. Is there any way to include a JS file in another while keeping all files on the local drive?

5
  • 1
    In what sort of environment is the code running? A browser running a local HTML file? Node? Commented Aug 10, 2021 at 4:17
  • It is running in a local HTML file, but functions from the included js file will be needed in the other, so adding another <script> tag with a different src will not work Commented Aug 10, 2021 at 4:21
  • 1
    It should, have you tried it? Just make sure the scripts run in the correct order. Yeah, it's not so intuitive, but top-level variables in one script file will be accessible by any other script file. Commented Aug 10, 2021 at 4:22
  • ah, it seems i made a typo in the file name, classic programming mistake, it does now work Commented Aug 10, 2021 at 4:25
  • And that my friend is why we love copy paste so much :) Commented Aug 10, 2021 at 4:52

1 Answer 1

1

In the html file, add 2 script tags, with the src property of the top one being to one you want to include in the other

<script src="include.js"></script>
<script src="code.js"></script>
Sign up to request clarification or add additional context in comments.

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.