0

I made long 2d game JavaScript file. I would like to divide it to multiple js file for each level so it will be easier to understand it and maintain it.

Is it possible to trigger a function from a js file that lies in another file? My thought is just to link to level_one script in html, and once the level_one is done, it will call the function in level_two script. Like a chain reaction/link.

I'm starting the game by

<body onload="startGame()">
4
  • 2
    yes. Have you tried it? Commented Nov 11, 2017 at 0:36
  • @traktor53 Oh I thought it would be more advanced. Just to be sure as I'm learning, is this a good way to keep js code organized, or should I consider other ways? Commented Nov 11, 2017 at 0:49
  • My suggestion is to review documentation of script tags to load external files and try simple examples to ensure calls between multiple files are working. Separating the code into different files may well be of benefit but remains a matter of choice and opinion - I would leave exploring advanced alternatives until after you are more comfortable with script files in general. Commented Nov 11, 2017 at 1:10
  • You can call functions that are declared in other javascript files. You just need to import both files in your html. Commented Nov 11, 2017 at 1:35

1 Answer 1

1

Short answer: Yes!

Long answer: there are many ways this is done, there are many libraries, frameworks, and paradigms people use. Maybe a good place for you to start would be to search for ways to include multiple files using pure JS. Start with small experiments. Have fun learning and good luck!

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.