0

I have a bunch of JavaScript functions, which I would like to have in separate JS files and be able to import them and use the functions in the main JS file like so

TestFunction.js:

function test(name) {
    console.log(name);
}

Main.js:

CODETOIMPORTHERE TestFunction.js
test('test!');
Jake Cross
  • 523
  • 1
  • 6
  • 14
  • Are you executing this in a browser ? Do you need a fancy module solution or would the usual simple solution be OK ? – Denys Séguret May 13 '15 at 10:53
  • The JavaScript would be executed in the browser yet. But I would like to be able to just import the code within the main.js file, as that would look a lot more clean if possible. – Jake Cross May 13 '15 at 10:54
  • If it's in a html document you can use `document.createElement('script')` and set the `src` attribute to the path where TestFunction.js is located. – pawel May 13 '15 at 10:55
  • So if I was to do something like: http://pastebin.com/FKrABDyc – Jake Cross May 13 '15 at 10:56
  • @IvankaTodorova Looked almost everywhere. Exactly what I needed. How can I mark this as a dupe? – Jake Cross May 13 '15 at 10:59
  • you can simple put reference of both files in your main `html` file – ozil May 13 '15 at 11:00
  • @JakeCross Be careful that most answers (including the accepted one) in the linked QA are terribly obsolete. Look at the answers referring to standard module solutions (AMD, requirejs, etc.) – Denys Séguret May 13 '15 at 11:02

0 Answers0