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.

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