Reputation: 9076
Do you embed JS in the body of your documents? Or do you put them all in the head/foot of the document only?
I have been putting them in a single site-wide file but it's becoming unmanageable. I'm thinking that using view helpers to selectively include the scripts into the body of the document could work nicely, but am I missing something?
Upvotes: 0
Views: 85
Reputation: 19830
For example I cannot put everything inside external JavaScript, because my server-side put information about localization into javascript. So I have:
<script>
XXX.lang = "de-DE";
</script>
Second thing is to generate during runtime or compilation time "bundled" scripts. There are a lots of tool for doing this for example: yui compressor. Using this tools can easy manage your code before of dev environment (many files) and have one file on production site.
Upvotes: 0
Reputation: 1908
i personally would put as few as possible js code into the document. Use module-libraries (AMD) for modularizing your code (this also only loads code which is really needed and does this also asynchron). use unobtrusive style of javascript to separate behaviour from design. (example)
Upvotes: 0
Reputation: 4987
The process that we follow is grouping the JS files logically and functionally across different files. We load only those files that are needed for the web page.
In case a JS file might or might not be required for your document, we use lazy loading of files, i.e. load files on demand. Facebook uses this concept extensively in their Bootloader framework approach.
This can be easily achieved. The main task for you is to split file into logical groups.
Hope this helps.
Upvotes: 0
Reputation: 9929
Try using a library like RequireJS to manage your .js files. This will add one script reference in your head but includes all other files when necessary. This will also support bundling and minification on build, if you need it.
As a sidenote; script blocks are always blocking the page load as the script is always executed before the load contineues. So, it is always best to use as minimal script blocks as possible and if you have them, place them right before the closing tag of your body to give the best user experience.
Upvotes: 4