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

CSS jQuery Basics (2014) Introduction to jQuery Ways to Include jQuery in a Project

David Diehr
David Diehr
16,457 Points

When and where to add script tags and jquery?

If I'm making a site with multiple pages, do I need to include script tags on every page (or on all the pages js used) or just the index page?

2 Answers

It depends, if you are only using the jQuery library and some associated jQuery code on a single page of your entire website, it would be unnecessary to load it on every single page, however, if you are using jQuery throughout the entire site most people generally load this within a footer file so that it is loaded just before the closing body tag on all pages of the website.

Henrik Hansen
Henrik Hansen
23,176 Points

You must load the script wherever it is used. Most ppl have catching enabled in the browser, so if you load the same files all over your site the performance will not be affected that much even with large files, and the jQuery is almost always already in cache as it is commonly used. I recommend loading all files in the head tag, and then an executing line of code in the end of the document, or wherever you need the execution to be made. There are events you can use as well, such as the onload event.

Read more at W3schools DOM events