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 trialEmma Sawyer
369 PointsI can place JS code directly in HTML file but linking to the math.js file through script tags not working?
Have not had this problem before, cannot find any errors in script tag writing <script src = "math.js"></script> ?
4 Answers
Bekah McDonald
10,527 PointsHi Emma
I can't see an error in your script tag. Is your math.js file saved in the same folder as your index.html file? If ever my JavaScript isn't working at all, it's because I've forgotten to include the whole file path (eg. script src="js/math.js"
)
Moderator marked as Best Answer
Steven Parker
231,269 PointsYour script tags look good.
Though in general you should avoid spaces around the equal sign when setting attributes in the tag.
Without seeing more of your project, a folder mismatch would be my first guess as to the cause of the issue. Check that your JS file is actually located in the same folder as your HTML. It's common to have a subfolder named "js" — perhaps it's there instead?
If that's not it, to facilitate a complete and thorough analysis, make a snapshot of your workspace and post the link to it here.
Emma Sawyer
369 Pointshttps://w.trhou.se/g31bt1h79e Here is a snap shot, my comment below elaborates
Steven Parker
231,269 PointsYour snapshot confirms the folder mismatch. You could move your file or just change the reference to it:
<script src="js/math.js">
Emma Sawyer
369 PointsI hope this link helps. The .js file is in the same workspace , all files I'm using are in the JS folder like it was already set up.
Bekah McDonald
10,527 PointsThanks for sharing your workspace.
While your .js file is in the same workspace, you've got it inside a folder called js. This is absolutely fine, but you just need to include that in your src, eg. <script src="js/math.js"></script>
It's exactly the same as what you're doing with your .css file. By including js/
in the src link, you're just telling it which folder contains your math.js
file.
Let me know if that helps!
Emma Sawyer
369 PointsThanks all, I changed the source to include the js/ folder and that worked! Somehow I missed that point way back. Thank you!!!
Steven Parker
231,269 PointsSteven Parker
231,269 PointsTo the Moderator:
To help me to improve the quality of future answers, could you explain what criteria was used to determine the chosen answer as "best"?
Haoz Bach Ly
3,709 PointsHaoz Bach Ly
3,709 PointsThank you very much, getting stuck with this matter for a while.