Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript

Micole Noddle
Micole Noddle
5,797 Points

How to link JavaScript files to HTML properly?

How many JS files can one link? I remember Dave saying only to use two, but I could be off, and I went back through the courses to try and find the answer, but couldn't seem to.

For instance, let's say we have a JS folder inside our main project folder and then an "app.js" file and a "students.js" file inside the JS folder. First of all, would this be common practice (to have two or more files in the JS folder)? Second of all, how would we link it to the HTML? Would we write two lines of code before the closing body tag such as:

<script src = "js/app.js"></script>

<script src = "js/students.js"></script>

Is this correct? Incorrect? Any help would be greatly appreciated. Thanks so much!

2 Answers

Your syntax is perfectly fine. And to answer your question, Yes, you can link more than 2 js files. For my project I had linked nearly 10 js files. Actually what you are doing here you are importing the javascript code to the html page. So the more js files you link the more will be the page load time.

Isaac Sanchez
Isaac Sanchez
3,126 Points

When linking JavaScript files you can have as many as you need. If they are in a folder maker sure you specify which one, like you did. You also don't have to write your code just at the end of the body tag, placing them at the end of the <head> tag is common.