3 ms·
In every real project I've done, where a nested templating system is used to generate the markup and templates might contain javascript code, I always load some
by DougWebb 11y ago
In every real project I've done, where a nested templating system is used to generate the markup and templates might contain javascript code, I always load some javascript libraries in the head. Typically that's Modernizr and jQuery, and if I'm using them jQuery Validate and Microsoft's jQuery Unobtrusive Validation.
This lets my templates contain initialization functions inside of $(document).ready(function() {...}); calls without running into a "$ not defined" error.
The alternative would be to somehow bubble up from my templates a list of initialization functions to call, or to have every page load run through a bunch of tests to find things that need to be initialized. I think the tradeoff of loading jQuery early is worth it, especially since I usually load it from Google's CDN so it's probably cached.