I believe that you want to write some kind of dependency tracking structure for your javascript files and use the "Lazy-Loading" process to download the necessary JS file only when necessary.
Check out Using.js , it seems to be doing what you need.
You can also check addModule on YUILoader . It allows you to load non-YUI infrastructure components on the fly.
Razvan caliman
source share