-1

I know this is a very basic, very newb question, but i cant seem to make it work.

I have 2 different javascript documents, one named movies and the other named calculator.

I would like to call calculator.js function, and put it inside my code in movies.js so that it would display it's result inside.

My code in movies.js is like this:

function formatState (movies) {
    if (!movies.id) { return movies.text; }
    var $movies = $(
    '<span><!--CALL JS. FUNCTION HERE--> ' + movies.text + '</span>'
    );
    return $movies;
};

my calculator.js

function calculate_price(value) {
//some data
}

at the end display result would be something like: 39.99€ star wars

Any help is appreciated!

23k
  • 1,596
  • 3
  • 23
  • 52
aiden87
  • 929
  • 8
  • 25
  • 52
  • As long as both JavaScript files are included on the web page then there is nothing special required, just call the function like normal. If you actually tried to do this and had a specific error or problem you should tell us about it. – takendarkk Aug 31 '17 at 19:46
  • i have multiple projects inside solution @csm_dev – aiden87 Aug 31 '17 at 19:47
  • So if calling the function directly doesn't work show us the error that you are getting. – takendarkk Aug 31 '17 at 19:48

2 Answers2

1

This isn't as basic of a question as you might think, because JS doesn't really have a clear pattern for this.

Check out the answer to this question.

Bricky
  • 2,572
  • 14
  • 30
0

Javascript does not support the concept of "includes" or "imports". If you want to do something like this you either:

  1. Reference both .js files in your HTML page - the order in which they are loaded is important.
  2. Use another library to help you with dynamic loading a Javascript file before executing any code. Require.js is an alternative. jQuery has a way to allow you to do this, but you will introduce another dependency...

More details here: How do I include a JavaScript file in another JavaScript file?

Icarus
  • 63,293
  • 14
  • 100
  • 115