I do a lot of work on large-scale ecommerce systems. Our backend-code and html are fairly compartmentalized into large chunks based on functionality for the major sections of the system - product (browse, detail), account, checkout, etc. There are section-specific files for JS, but they are currently being concatenated and minified into a single file, to minimize HTTP requests.
In writing jQuery for our system, we are trying to determine the best way to unobtrusively associate section-specific JS with each part of our system. A few schools of thought are as follows:
Run functions on DOM queries even if it is known that those DOM elements may not exist in every given page / section. For example,
$('.checkout-submit').doSomething();
This CSS class will most likely only exist in checkout, but if we do add it anywhere else on the site, it will automatically sync up, site-wide.Create initialization functions for each major section (account, checkout, etc.), and call those init functions on the pages which require them, directly in the html. For example,
$.initCheckout();
Remove our concatenation process, and include section-specific files only when necessary. This would reduce the core of the problem, but would add to HTTP requests, especially considering that we would then need to have an additional file for global code, libraries, etc.
Additional suggestions beyond what is listed here are more than welcome. Thanks!