Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Basics Working with Strings Introducing Strings

Chelsea Byerly
Chelsea Byerly
175 Points

using js/ before the file name vs. not when linking a file via script tag

I've noticed that in some tutorials, we are instructed to indicate js/ before the file name when linking another file in a script tag. For example, <script src="js/strings.js"></script>, but in other tutorials, that seems to be left out. For example, <script src="strings.js></script>. The video above uses the former method, yet during a coding challenge, I was marked incorrect for using the js/ before the file name. Some clarification on this would be appreciated. Thanks!

1 Answer

Michael Hulet
Michael Hulet
47,913 Points

The src attribute (like many other HTML attributes, such as href) takes a file locator, which can be as specific as a full URL or as flexible as a file path relative to the current document, like it sounds like youโ€™re usually doing.

If your JavaScript file lives in the same folder as your HTML file on the server, all you need to reference it is just the name of the JavaScript file. However, if your HTML file is in the same folder as another folder which in turn holds your JavaScript file, then it would be appropriate to prefix the name of the file with the name of the folder that contains it followed by a forward slash (/)

For example, letโ€™s say I have an HTML file called index.html and a JavaScript file in the same folder on the server called script.js. I might include that JavaScript file in that HTML by adding a tag like this to index.html:

index.html
<script src=โ€œscript.jsโ€></script>

However, if script.js was contained in a folder called js, and that folder is in the same folder as index.html, I might include it like this:

index.html
<script src=โ€œjs/script.jsโ€></script>